Last active
April 26, 2023 14:06
-
-
Save RitwikGA/69f1a17482f1c457f729111d14dcc529 to your computer and use it in GitHub Desktop.
Acquisition Dimension in Google Analytics - GTM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
/* UTM Tracking - Acquisition/First Attribution Custom Dimension - GTM | |
* @Ritwikga | |
*/ | |
var cookie_name = 'ds_utm' //Name of Cookie | |
var cookie_duration = 180 //days | |
var queryParam = window.location.search | |
if(queryParam.search('utm_source') > -1 && queryParam.search('utm_medium') > -1) | |
{ | |
cookieModify(cookie_name, true) | |
} else {cookieModify(cookie_name, false)} | |
function cookieModify(cookie_name, utms) | |
{ | |
if(!readCookie(cookie_name)) | |
{ | |
if(utms) | |
{ | |
var source = /utm_source=([^&]+)/g.exec(queryParam)[1] | |
var medium = /utm_medium=([^&]+)/g.exec(queryParam)[1] | |
var campaign = /utm_campaign=([^&]+)/g.exec(queryParam)?/utm_campaign=([^&]+)/g.exec(queryParam)[1]:'undefined' | |
} else | |
{ | |
var source = (document.referrer == "") ? "direct" : document.referrer | |
var medium = (source === "direct") ? "none" : "" | |
var campaign = "none" | |
if(queryParam.search('gclid=') > -1) | |
{source = "google";medium = "cpc"} | |
if(queryParam.search('fbclid=') > -1) | |
{source = "facebook.com";medium = "referral"} | |
if(source.search(/www\.google|www\.bing|duckduckgo|yandex/g) > -1) | |
{medium = "organic"} | |
source = source.replace(/\//g,'').replace(/http:|https:/,'') //remove https:// | |
} | |
var date = new Date(); | |
var dateArray = date.toLocaleDateString('en-US').split('/') | |
var acquisitionDate = dateArray[2]+("0" + dateArray[0]).slice(-2)+("0" + dateArray[1]).slice(-2) | |
var acquisitionMonth = ("0" + dateArray[0]).slice(-2) | |
var acquisitionContent = location.pathname | |
var cookie_value = 'utm_source='+source+'&utm_medium='+medium+'&utm_campaign='+campaign+'&utm_content='+location.pathname+'&utm_date='+acquisitionDate+'&utm_month='+acquisitionMonth; | |
createCookie(cookie_name, btoa(cookie_value) ,cookie_duration) | |
} | |
else | |
{ | |
var old_utms = readCookie(cookie_name) | |
old_utms = atob(old_utms) | |
var source = /utm_source=([^&]+)/g.exec(old_utms)[1] | |
var medium = /utm_medium=([^&]+)/g.exec(old_utms)[1] | |
var campaign = /utm_campaign=([^&]+)/g.exec(old_utms)?/utm_campaign=([^&]+)/g.exec(old_utms)[1]:'undefined' | |
var acquisitionContent = /utm_content=([^&]+)/g.exec(old_utms)[1] | |
var acquisitionDate = /utm_date=([^&]+)/g.exec(old_utms)[1] | |
var acquisitionMonth = /utm_month=([^&]+)/g.exec(old_utms)[1] | |
} | |
dataLayer.push({'event':'firstAttribution','acquisitionSource': source ,'acquisitionMedium': medium, 'acquisitionCampaign': campaign, 'acquisitionContent':acquisitionContent, 'acquisitionDate': acquisitionDate,'acquisitionMonth': acquisitionMonth }) | |
} | |
// Source : https://www.quirksmode.org/js/cookies.html /// | |
function createCookie(name,value,days) { | |
if (days) { | |
var date = new Date(); | |
date.setTime(date.getTime()+(days*24*60*60*1000)); | |
var expires = "; expires="+date.toGMTString(); | |
} | |
else {var expires = "";} | |
var domainName = /\.[^\s]+/g.exec(location.hostname)[0] | |
if(location.hostname.match(/\./g).length == 1){ | |
domainName = location.hostname; | |
} | |
document.cookie = name+"="+value+expires+"; path=/; domain="+domainName; | |
} | |
function readCookie(name) { | |
var nameEQ = name + "="; | |
var ca = document.cookie.split(';'); | |
for(var i=0;i < ca.length;i++) { | |
var c = ca[i]; | |
while (c.charAt(0)==' ') c = c.substring(1,c.length); | |
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); | |
} | |
return null; | |
} | |
function eraseCookie(name) { | |
createCookie(name,"",-1); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment