Skip to content

Instantly share code, notes, and snippets.

@measureschool
Created August 17, 2018 07:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save measureschool/25486df29a0e86cf34d5f4075af986e6 to your computer and use it in GitHub Desktop.
Save measureschool/25486df29a0e86cf34d5f4075af986e6 to your computer and use it in GitHub Desktop.
<script>
(function() {
var s = document.location.search;
var h = document.location.hash;
var e = {{Event}};
var n = {{New History Fragment}};
var o = {{Old History Fragment}};
// Only run if the History API is supported
if (window.history) {
// Create a new history state if the user lands from Google's SERP
if (e === 'gtm.js' &&
document.referrer.indexOf('www.google.') > -1 &&
s.indexOf('gclid') === -1 &&
s.indexOf('utm_') === -1 &&
h !== '#gref') {
window.oldFragment = false;
window.history.pushState(null,null,'#gref');
} else if (e === 'gtm.js') {
window.oldFragment = true;
}
// When the user tries to return to the SERP using browser back, fire the
// Google Analytics timing event, and after it's dispatched, manually
// navigate to the previous history entry, i.e. the SERP
if (e === 'gtm.historyChange' &&
n === '' &&
o === 'gref') {
var time = new Date().getTime() - {{DLV - gtm.start}};
if (!window.oldFragment) {
dataLayer.push({
'event' : 'returnToSerp',
'timeToSerp' : time,
'eventCallback' : function() {
window.history.go(-1);
}
});
} else {
window.history.go(-1);
}
}
}
})();
</script>
{
"exportFormatVersion": 2,
"exportTime": "2018-08-17 07:43:30",
"containerVersion": {
"path": "accounts/144483319/containers/9615777/versions/2",
"accountId": "144483319",
"containerId": "9615777",
"containerVersionId": "2",
"container": {
"path": "accounts/144483319/containers/9615777",
"accountId": "144483319",
"containerId": "9615777",
"name": "209SERPBounce",
"publicId": "GTM-TKBKPJB",
"usageContext": [
"WEB"
],
"fingerprint": "1533627839762",
"tagManagerUrl": "https://tagmanager.google.com/#/container/accounts/144483319/containers/9615777/workspaces?apiLink=container"
},
"tag": [
{
"accountId": "144483319",
"containerId": "9615777",
"tagId": "4",
"name": "GA - Timing - BackToSERP",
"type": "ua",
"parameter": [
{
"type": "BOOLEAN",
"key": "overrideGaSettings",
"value": "true"
},
{
"type": "TEMPLATE",
"key": "trackType",
"value": "TRACK_TIMING"
},
{
"type": "TEMPLATE",
"key": "timingCategory",
"value": "Back To SERP time"
},
{
"type": "TEMPLATE",
"key": "timingVar",
"value": "{{Page Path}}"
},
{
"type": "TEMPLATE",
"key": "timingValue",
"value": "{{DLV - Time to SERP}}"
},
{
"type": "TEMPLATE",
"key": "trackingId",
"value": "UA-12345-46"
}
],
"fingerprint": "1534321313768",
"firingTriggerId": [
"9"
],
"tagFiringOption": "ONCE_PER_EVENT"
},
{
"accountId": "144483319",
"containerId": "9615777",
"tagId": "3",
"name": "Listener - SERP Bounce",
"type": "html",
"parameter": [
{
"type": "TEMPLATE",
"key": "html",
"value": "<script>\n (function() {\n var s = document.location.search;\n var h = document.location.hash;\n var e = {{Event}};\n var n = {{New History Fragment}};\n var o = {{Old History Fragment}};\n \n // Only run if the History API is supported\n if (window.history) {\n\n // Create a new history state if the user lands from Google's SERP\n if (e === 'gtm.js' && \n document.referrer.indexOf('www.google.') > -1 && \n s.indexOf('gclid') === -1 &&\n s.indexOf('utm_') === -1 &&\n h !== '#gref') {\n window.oldFragment = false;\n window.history.pushState(null,null,'#gref');\n } else if (e === 'gtm.js') {\n window.oldFragment = true;\n }\n\n // When the user tries to return to the SERP using browser back, fire the\n // Google Analytics timing event, and after it's dispatched, manually\n // navigate to the previous history entry, i.e. the SERP\n \n if (e === 'gtm.historyChange' && \n n === '' && \n o === 'gref') {\n var time = new Date().getTime() - {{DLV - gtm.start}};\n if (!window.oldFragment) {\n dataLayer.push({\n 'event' : 'returnToSerp',\n 'timeToSerp' : time,\n 'eventCallback' : function() {\n window.history.go(-1);\n }\n });\n } else {\n window.history.go(-1);\n }\n }\n \n }\n })();\n</script>"
},
{
"type": "BOOLEAN",
"key": "supportDocumentWrite",
"value": "false"
}
],
"fingerprint": "1534491790424",
"firingTriggerId": [
"8",
"2147479553"
],
"tagFiringOption": "ONCE_PER_EVENT"
}
],
"trigger": [
{
"accountId": "144483319",
"containerId": "9615777",
"triggerId": "9",
"name": "custom - BackToSerp",
"type": "CUSTOM_EVENT",
"customEventFilter": [
{
"type": "EQUALS",
"parameter": [
{
"type": "TEMPLATE",
"key": "arg0",
"value": "{{_event}}"
},
{
"type": "TEMPLATE",
"key": "arg1",
"value": "returnToSerp"
}
]
}
],
"filter": [
{
"type": "LESS",
"parameter": [
{
"type": "TEMPLATE",
"key": "arg0",
"value": "{{DLV - Time to SERP}}"
},
{
"type": "TEMPLATE",
"key": "arg1",
"value": "1800000"
}
]
}
],
"fingerprint": "1533627676735"
},
{
"accountId": "144483319",
"containerId": "9615777",
"triggerId": "8",
"name": "history - back to SERP",
"type": "HISTORY_CHANGE",
"filter": [
{
"type": "EQUALS",
"parameter": [
{
"type": "TEMPLATE",
"key": "arg0",
"value": "{{History Source}}"
},
{
"type": "TEMPLATE",
"key": "arg1",
"value": "popstate"
}
]
},
{
"type": "MATCH_REGEX",
"parameter": [
{
"type": "TEMPLATE",
"key": "arg0",
"value": "{{New History Fragment}}"
},
{
"type": "TEMPLATE",
"key": "arg1",
"value": "^$"
}
]
}
],
"fingerprint": "1533627182067"
}
],
"variable": [
{
"accountId": "144483319",
"containerId": "9615777",
"variableId": "4",
"name": "DLV - Time to SERP",
"type": "v",
"parameter": [
{
"type": "INTEGER",
"key": "dataLayerVersion",
"value": "2"
},
{
"type": "BOOLEAN",
"key": "setDefaultValue",
"value": "false"
},
{
"type": "TEMPLATE",
"key": "name",
"value": "timeToSerp"
}
],
"fingerprint": "1533627512672"
},
{
"accountId": "144483319",
"containerId": "9615777",
"variableId": "3",
"name": "DLV - gtm.start",
"type": "v",
"parameter": [
{
"type": "INTEGER",
"key": "dataLayerVersion",
"value": "2"
},
{
"type": "BOOLEAN",
"key": "setDefaultValue",
"value": "false"
},
{
"type": "TEMPLATE",
"key": "name",
"value": "gtm.start"
}
],
"fingerprint": "1533627318754"
}
],
"builtInVariable": [
{
"accountId": "144483319",
"containerId": "9615777",
"type": "PAGE_URL",
"name": "Page URL"
},
{
"accountId": "144483319",
"containerId": "9615777",
"type": "PAGE_HOSTNAME",
"name": "Page Hostname"
},
{
"accountId": "144483319",
"containerId": "9615777",
"type": "PAGE_PATH",
"name": "Page Path"
},
{
"accountId": "144483319",
"containerId": "9615777",
"type": "REFERRER",
"name": "Referrer"
},
{
"accountId": "144483319",
"containerId": "9615777",
"type": "EVENT",
"name": "Event"
},
{
"accountId": "144483319",
"containerId": "9615777",
"type": "NEW_HISTORY_FRAGMENT",
"name": "New History Fragment"
},
{
"accountId": "144483319",
"containerId": "9615777",
"type": "OLD_HISTORY_FRAGMENT",
"name": "Old History Fragment"
},
{
"accountId": "144483319",
"containerId": "9615777",
"type": "HISTORY_SOURCE",
"name": "History Source"
}
],
"fingerprint": "1534491801857",
"tagManagerUrl": "https://tagmanager.google.com/#/versions/accounts/144483319/containers/9615777/versions/2?apiLink=version"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment