Tuesday, June 26, 2018

PeopleTools - Opt-out option for Google Analytics

With GDPR taking effect on May 25th 2018, an "opt-out" option for tracking cookies is a prevalent topic in many forums. This post is not intended to provide guidance for GDPR compliance. FWIW, before implementing anything at the PeopleTools layer, we must first consider the privacy policy implications at a much broader organizational level.

That said, I have written several posts on how to implement Google Analytics in PeopleTools. The most recent approach uses gtag.js.
PeopleTools - Using Google Analytics (gtag.js)

This post describes a proof of concept to extend the gtag.js implementation to provide a simple "opt-out" feature for end users using client side code (javascript).

Demo

In the following video, we can see that the user selects the "opt-out" option and the custom opt-out cookie is created. Any subsequent requests will not be tracked in Google Analytics.


Google Analytics Real-Time Data


In the following video, we can see that the user dismisses the "opt-out" option and the custom opt-out cookie is not created. Any subsequent requests will continue to be tracked in Google Analytics.


Google Analytics Real-Time Data


Script for reference

function loadScript(url, callback){
var script = document.createElement("script")
script.type = "text/javascript";
if (script.readyState){ //IE
script.onreadystatechange = function(){
if (script.readyState == "loaded" ||
script.readyState == "complete"){
script.onreadystatechange = null;
callback();
}
};
} else { //Others
script.onload = function(){
callback();
};
}
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
}
// Set to the same value as the web property used on the site
var gaProperty = 'UA-XXXXXXXX-Y';
// Disable tracking if the opt-out cookie exists.
var disableStr = 'ga-disable-' + gaProperty;
// Opt-out function
function gaOptout() {
// console.log("gaOptout");
document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
window[disableStr] = true;
ptCloseConfirmation();
}
window.onload = function () {
// console.log("Google Analytics debug");
// If custom ga-disable-... cookie exists, disable Google Analytics
if (document.cookie.indexOf(disableStr + '=true') > -1) {
window[disableStr] = true;
}
// Display opt-out message if new session (ga cookies do not exist)
if ((document.cookie.indexOf('_ga') == -1) && (document.cookie.indexOf(disableStr + '=true') == -1)) {
// console.log("Cookie does not exist");
msg = "This site uses Google Analytics." + "&nbsp;<a href='javascript:gaOptout()'>Click here to opt-out</a>";
ptSetConfirmationMessage(msg, "", "psc_confirmation-noautodismiss psc_open");
}
// Execute Google Analytics Code
loadScript("https://www.googletagmanager.com/gtag/js?id=" + gaProperty, function(){
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', gaProperty);
});
};
Notes

- This version of javascript, identifies the existence of Google Analytics related cookies to check if the session is new.
- If the session is new, a popup message is displayed to the end user. The message also provides a link to "opt-out" of Google Analytics. For the purposes of this demo, I used the delivered ptSetConfirmationMessage and ptCloseConfirmation javascript functions (which are part of PT_UTIL_BASIC HTML object) to display this message.
- If the user chooses to "opt-out" of Google Analytics, the following window property is set to true.
window['ga-disable-UA-XXXXXX-Y'] = true;
- Additionally, a custom cookie (ga-disable-UA-XXXXXXXX-Y) is created to identify this choice for subsequent requests.
- This cookie preference/choice will persist as long as this custom cookie is present.

This proof of concept is only intended to provide implementation ideas. You will most certainly need to tweak this code based on your organization's privacy policy.

Useful Resources

https://developers.google.com/analytics/devguides/collection/analyticsjs/user-opt-out
https://developers.google.com/analytics/devguides/collection/gajs/
https://www.robert-matthees.de/datenschutz-cookies/

Related Posts

Implementing Google Analytics in PeopleTools using gtag.js
Implementing Google Analytics in PeopleTools using analytics.js
Implementing Google Analytics in PeopleTools using ga.js