Skip to content

Instantly share code, notes, and snippets.

@ju1
Created February 26, 2017 20:33
Show Gist options
  • Save ju1/dc76fd06ecbb371b907bf5e15efce034 to your computer and use it in GitHub Desktop.
Save ju1/dc76fd06ecbb371b907bf5e15efce034 to your computer and use it in GitHub Desktop.
Form Abandonment Recipe for Google Tag Manager
{
"exportFormatVersion": 2,
"exportTime": "2017-02-26 20:31:25",
"containerVersion": {
"path": "accounts/1164259306/containers/6423900/versions/0",
"accountId": "1164259306",
"containerId": "6423900",
"containerVersionId": "0",
"container": {
"path": "accounts/1164259306/containers/6423900",
"accountId": "1164259306",
"containerId": "6423900",
"name": "Form Abandonment Recipe",
"publicId": "GTM-KVLRZ75",
"usageContext": [
"WEB"
],
"fingerprint": "1488140805623",
"tagManagerUrl": "https://tagmanager.google.com/#/container/accounts/1164259306/containers/6423900/workspaces?apiLink=container"
},
"tag": [
{
"accountId": "1164259306",
"containerId": "6423900",
"tagId": "2",
"name": "GA - Event - Form Abandonment",
"type": "ua",
"parameter": [
{
"type": "BOOLEAN",
"key": "nonInteraction",
"value": "true"
},
{
"type": "BOOLEAN",
"key": "doubleClick",
"value": "false"
},
{
"type": "BOOLEAN",
"key": "setTrackerName",
"value": "false"
},
{
"type": "BOOLEAN",
"key": "useDebugVersion",
"value": "false"
},
{
"type": "LIST",
"key": "fieldsToSet",
"list": [
{
"type": "MAP",
"map": [
{
"type": "TEMPLATE",
"key": "fieldName",
"value": "transport"
},
{
"type": "TEMPLATE",
"key": "value",
"value": "beacon"
}
]
}
]
},
{
"type": "TEMPLATE",
"key": "eventCategory",
"value": "{{dlv - eventCategory}}"
},
{
"type": "TEMPLATE",
"key": "trackType",
"value": "TRACK_EVENT"
},
{
"type": "BOOLEAN",
"key": "enableLinkId",
"value": "false"
},
{
"type": "TEMPLATE",
"key": "eventAction",
"value": "{{dlv - eventAction}}"
},
{
"type": "TEMPLATE",
"key": "eventLabel",
"value": "{{Page URL}}"
},
{
"type": "BOOLEAN",
"key": "enableEcommerce",
"value": "false"
},
{
"type": "TEMPLATE",
"key": "trackingId",
"value": "{{YOUR_GA_TRACKING_ID}}"
}
],
"fingerprint": "1488141067718",
"firingTriggerId": [
"5"
],
"tagFiringOption": "ONCE_PER_EVENT"
},
{
"accountId": "1164259306",
"containerId": "6423900",
"tagId": "1",
"name": "cHTML - Form Abandonment Listener",
"type": "html",
"parameter": [
{
"type": "TEMPLATE",
"key": "html",
"value": "<script>\n(function() {\n if (typeof document.querySelectorAll === \"undefined\") {\n return\n }\n window.addEventListener('beforeunload', function(e) {\n findUnsubmittedForms().forEach(function(it) {\n window.dataLayer.push({\n 'event' : 'formAbandonment',\n 'eventCategory' : 'Form Abandonment',\n 'eventAction' : it.history.join(\" > \")\n })\n })\n })\n\n var history = {}\n\n window.addEventListener(\"load\", function() {\n document.addEventListener(\"change\", function(e) {\n var target = e.target\n if (target && target.tagName && (target.tagName.toUpperCase() == \"INPUT\" || target.tagName.toUpperCase() == \"TEXTAREA\" || target.tagName.toUpperCase() == \"SELECT\")) {\n var inputName = target.getAttribute(\"name\")\n var form = target.form\n if (form && inputName) {\n var formName = form.getAttribute(\"name\")\n if (typeof history[formName] == \"undefined\") {\n history[formName] = []\n }\n if (history[formName].slice(-1) != inputName) {\n history[formName].push(inputName)\n }\n }\n }\n })\n })\n\n function findUnsubmittedForms() {\n return Object.keys(history).filter(hasNoFormSubmitEvent(window.dataLayer)).map(findFormFromHistory).filter(notEmpty)\n }\n\n function hasNoFormSubmitEvent(dataLayer) {\n return function(name) {\n return dataLayer.filter(isFormSubmitEvent).map(getFormName).indexOf(name) == -1\n }\n }\n\n function isFormSubmitEvent(e) {\n return e.event === 'gtm.formSubmit'\n }\n\n function getFormName(e) {\n return e['gtm.element'].name\n }\n\n function findFormFromHistory(name) {\n return {\n name: name,\n history: (history[name] || [])\n }\n }\n\n function notEmpty(form) {\n return form.history.length > 0\n }\n\n})()\n</script>"
},
{
"type": "BOOLEAN",
"key": "supportDocumentWrite",
"value": "false"
}
],
"fingerprint": "1488140220664",
"firingTriggerId": [
"2147479553"
],
"tagFiringOption": "ONCE_PER_EVENT"
}
],
"trigger": [
{
"accountId": "1164259306",
"containerId": "6423900",
"triggerId": "5",
"name": "Custom - Form Abandonment",
"type": "CUSTOM_EVENT",
"customEventFilter": [
{
"type": "EQUALS",
"parameter": [
{
"type": "TEMPLATE",
"key": "arg0",
"value": "{{_event}}"
},
{
"type": "TEMPLATE",
"key": "arg1",
"value": "formAbandonment"
}
]
}
],
"fingerprint": "1488140757390"
}
],
"variable": [
{
"accountId": "1164259306",
"containerId": "6423900",
"variableId": "2",
"name": "dlv - eventAction",
"type": "v",
"parameter": [
{
"type": "INTEGER",
"key": "dataLayerVersion",
"value": "2"
},
{
"type": "BOOLEAN",
"key": "setDefaultValue",
"value": "false"
},
{
"type": "TEMPLATE",
"key": "name",
"value": "eventAction"
}
],
"fingerprint": "1488140797126"
},
{
"accountId": "1164259306",
"containerId": "6423900",
"variableId": "1",
"name": "dlv - eventCategory",
"type": "v",
"parameter": [
{
"type": "INTEGER",
"key": "dataLayerVersion",
"value": "2"
},
{
"type": "BOOLEAN",
"key": "setDefaultValue",
"value": "false"
},
{
"type": "TEMPLATE",
"key": "name",
"value": "eventCategory"
}
],
"fingerprint": "1488140781977"
}
],
"builtInVariable": [
{
"accountId": "1164259306",
"containerId": "6423900",
"type": "PAGE_URL",
"name": "Page URL"
},
{
"accountId": "1164259306",
"containerId": "6423900",
"type": "PAGE_HOSTNAME",
"name": "Page Hostname"
},
{
"accountId": "1164259306",
"containerId": "6423900",
"type": "PAGE_PATH",
"name": "Page Path"
},
{
"accountId": "1164259306",
"containerId": "6423900",
"type": "REFERRER",
"name": "Referrer"
},
{
"accountId": "1164259306",
"containerId": "6423900",
"type": "EVENT",
"name": "Event"
}
],
"fingerprint": "0",
"tagManagerUrl": "https://tagmanager.google.com/#/versions/accounts/1164259306/containers/6423900/versions/0?apiLink=version"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment