Skip to content

Instantly share code, notes, and snippets.

@mbaersch
Last active July 16, 2020 14:16
Show Gist options
  • Save mbaersch/70bb1c1ee2ea4787544d1ed395de1727 to your computer and use it in GitHub Desktop.
Save mbaersch/70bb1c1ee2ea4787544d1ed395de1727 to your computer and use it in GitHub Desktop.
Upgrade Your Analytics Cookies to fight ITP2.1:
/* GTM : use the following code inside a customTask, hitCallback oder a cleanup tag
GA.JS : modify your existing tracking code with a customTask or hitCallback and execute the following code
GTAG.JS : modify your existing tracking code with a manual page_view event
including an event_callback (gtag.js) and execute the following code
customTask ga.js - see: https://developers.google.com/analytics/devguides/collection/analyticsjs/tasks#adding_to_a_task
hitCallback ga.js - see: https://developers.google.com/analytics/devguides/collection/analyticsjs/sending-hits#hitcallback
event_callback gtag.js - see: https://developers.google.com/analytics/devguides/collection/gtagjs/sending-data#implement_event_callback_functions
*/
//"harden" _ga cookie by calling server-side cookie renewal script to upgrade cookie
var ua = navigator.userAgent.toLowerCase();
var req = null;
//check browser - upgrade only for Safari users. This condition is optional as other browsers like Firefox wll likely follow
if ((ua.indexOf('safari') != -1) && (ua.indexOf('chrome') < 0)) {
if (window.XMLHttpRequest) req = new XMLHttpRequest();
if (req != null) {
//configured "upgrade_ga_cookie.php" script on your server required in order to work!
//see https://gist.github.com/mbaersch/70bb1c1ee2ea4787544d1ed395de1727 for details
req.open("GET", "/upgrade_ga_cookie.php", true);
req.send();
}
}
<!-- complete example for gtag.js tracking code with cookie renewal event-callback to upgrade_ga_cookie.php
(see example below) and deactvated cookie_update through ga´s tracking code. Sending the automatic pageview
is deactvated and a separate event ist used to add the callback function. Uses own cookie to renew _ga cookie
server-side every 6 days - see Simo Ahava´s example at
https://disqus.com/home/discussion/simoahava/create_a_cookie_rewrite_web_service_using_the_google_cloud_platform/
usage: replace (both!) UA-1234567890-1 with your own property id -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-1234567890-1"></script>
<script>
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-1234567890-1', {'cookie_update': false, 'anonymize_ip': true, 'send_page_view': false});
gtag('event', 'page_view', {
'event_callback': function() {
var cid = getCookie('_ga');
var req = null;
var didUpdate = (getCookie('ga_updated') === "true");
if (cid && !didUpdate) {
if (window.XMLHttpRequest) req = new XMLHttpRequest();
if (req != null) {
req.open("GET", "/upgrade_ga_cookie.php", true);
req.send();
document.cookie = 'ga_updated=true;path=/;max-age=' + 60*60*24*6;
}
}
}
});
</script>
<!-- complete example for analytics.js tracking code with cookie renewal hitCallback to upgrade_ga_cookie.php
(see example below) and deactivated cookie_update through ga´s tracking code. Uses own cookie to renew _ga cookie
server-side every 6 days - see Simo Ahava´s example at
https://disqus.com/home/discussion/simoahava/create_a_cookie_rewrite_web_service_using_the_google_cloud_platform/
usage: replace UA-1234567890-1 with your own property id -->
<script>
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-1234567890-1', 'auto', {cookieUpdate: false});
ga('set', 'anonymizeIp', true);
ga('send', 'pageview', {
hitCallback: function() {
var cid = getCookie('_ga');
var req = null;
var didUpdate = (getCookie('ga_updated') === "true");
if (cid && !didUpdate) {
if (window.XMLHttpRequest) req = new XMLHttpRequest();
if (req != null) {
req.open("GET", "/upgrade_ga_cookie.php", true);
req.send();
document.cookie = 'ga_updated=true;path=/;max-age=' + 60*60*24*6;
}
}
}
});
</script>
@mbaersch
Copy link
Author

mbaersch commented Jun 12, 2019

As described in Simo Ahava´s blogpost about a rewrite web service the number of "renewal calls" can be reduced by stopping GA from updating the cookie every time. Remembering the last update in a separate cookie and only sending the request if neccessary can be achieved in the same way. The only difference would be that no web service gets called but the upgrade_ga_cookie.php on your own server.

I added two examples for gtag.js and analytics.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment