Integrating Your Actions

This page shows how you can integrate your actions for event handling.

Event Handling:

Examples:

  1. Load marketing cookies if the user has accepted:

<script type="text/javascript">
window.addEventListener('GdprlCookieConsentOnAccept', function (e) {
        if(e.detail.wpl_viewed_cookie == 'yes') {
        	if(e.detail.wpl_user_preference.marketing == 'yes') {
                // Execute code that sets marketing cookies.
            }
        }
}, false);
</script>

2. Load Google Analytics if the user has accepted Cookies:

<script type="text/javascript">
window.addEventListener('GdprCookieConsentOnAccept', function (e) {
        if(e.detail.wpl_viewed_cookie == 'yes') {
        	if(e.detail.wpl_user_preference.analytics == 'yes') {
                enableGoogleAnalytics();
            }
       }
}, false);
var _gaq = [];
function enableGoogleAnalytics() {
    _gaq.push(['_setAccount', 'UA-00000000-0']);
    _gaq.push(['_trackPageview']);
    (function () {
        var ga = document.createElement('script');
        ga.type = 'text/javascript';
        ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(ga, s);
    })();
}
</script>

3. PHP Code:

if(isset($_COOKIE['wpl_viewed_cookie'])) {
switch($_COOKIE['wpl_viewed_cookie']) {
		case 'yes' :
		// the user has accepted one or more type of cookies.
		$valid_php_json = preg_replace('/\s*:\s*([a-zA-Z0-9_]+?)([}\[,])/', ':"$1"$2', preg_replace('/([{\[,])\s*([a-zA-Z0-9_]+?):/', '$1"$2":', str_replace("'", '"',stripslashes($_COOKIE['wpl_user_preference']))));
		$wpl_user_preference = json_decode($valid_php_json);
		if($wpl_user_preference->analytics === 'yes') {
			// user has accepted analytics cookies.
		} else {
			// user has not accepted analytics cookies.
		}
		if($wpl_user_preference->marketing === 'yes') {
			// user has accepted marketing cookies.
		} else {
			// user has not accepted marketing cookies.
		}
		if($wpl_user_preference->preferences === 'yes') {
			// user has accepted preferences cookies.
		} else {
			// user has not accepted preferences cookies.
		}
		break;
		case 'no' :
		// the user has not accepted cookies - set necessary cookies only.
		break;
		default :
		break;
	}
} else {
	// the user has not accepted cookies - set necessary cookies only.
}

Last updated