Last active
March 6, 2024 06:21
-
-
Save kmclaugh/fad1431b1916f3776814f1a248f7a4b8 to your computer and use it in GitHub Desktop.
Notifies the datalyer when a wpForm is submitted so Google Tag Manager can detect it
This file contains 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
// This code notifies Google Tag Manager when a wpForm is submitted | |
document.addEventListener("DOMContentLoaded", function() { | |
var elementsArray = document.querySelectorAll('[id^="wpforms-form-"]'); | |
elementsArray.forEach(function(elem) { | |
elem.addEventListener("submit", function(e) { | |
window.dataLayer = window.dataLayer || []; | |
window.dataLayer.push({ | |
event: "wpFormSubmit", | |
wpFormElement: event.target | |
}); | |
}); | |
}); | |
}); |
This file contains 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
{ | |
"exportFormatVersion": 2, | |
"exportTime": "2019-06-24 16:13:41", | |
"containerVersion": { | |
"path": "accounts/4701996609/containers/11992448/versions/0", | |
"accountId": "4701996609", | |
"containerId": "11992448", | |
"containerVersionId": "0", | |
"container": { | |
"path": "accounts/4701996609/containers/11992448", | |
"accountId": "4701996609", | |
"containerId": "11992448", | |
"name": "temp", | |
"publicId": "GTM-WP4RRDJ", | |
"usageContext": [ | |
"WEB" | |
], | |
"fingerprint": "1561392791555", | |
"tagManagerUrl": "https://tagmanager.google.com/#/container/accounts/4701996609/containers/11992448/workspaces?apiLink=container" | |
}, | |
"tag": [ | |
{ | |
"accountId": "4701996609", | |
"containerId": "11992448", | |
"tagId": "7", | |
"name": "GA - Form Submit", | |
"type": "ua", | |
"parameter": [ | |
{ | |
"type": "BOOLEAN", | |
"key": "nonInteraction", | |
"value": "false" | |
}, | |
{ | |
"type": "BOOLEAN", | |
"key": "overrideGaSettings", | |
"value": "false" | |
}, | |
{ | |
"type": "TEMPLATE", | |
"key": "eventCategory", | |
"value": "Forms" | |
}, | |
{ | |
"type": "TEMPLATE", | |
"key": "trackType", | |
"value": "TRACK_EVENT" | |
}, | |
{ | |
"type": "TEMPLATE", | |
"key": "gaSettings", | |
"value": "{{GA UTC}}" | |
}, | |
{ | |
"type": "TEMPLATE", | |
"key": "eventAction", | |
"value": "{{wpFormAction}}" | |
}, | |
{ | |
"type": "TEMPLATE", | |
"key": "eventLabel", | |
"value": "{{wpFormId}}" | |
} | |
], | |
"fingerprint": "1561387059063", | |
"firingTriggerId": [ | |
"17" | |
], | |
"parentFolderId": "7", | |
"tagFiringOption": "ONCE_PER_EVENT" | |
}, | |
{ | |
"accountId": "4701996609", | |
"containerId": "11992448", | |
"tagId": "8", | |
"name": "wpForm Add Event Listener", | |
"type": "html", | |
"parameter": [ | |
{ | |
"type": "TEMPLATE", | |
"key": "html", | |
"value": "<script>\n // This code notifies Google Tag Manager when a wpForm is submitted\n var elementsArray = document.querySelectorAll('[id^=\"wpforms-form-\"]');\n elementsArray.forEach(function(elem) {\n elem.addEventListener(\"submit\", function(e) {\n window.dataLayer = window.dataLayer || [];\n window.dataLayer.push({\n event: \"wpFormSubmit\",\n wpFormElement: event.target\n });\n });\n });\n</script>" | |
}, | |
{ | |
"type": "BOOLEAN", | |
"key": "supportDocumentWrite", | |
"value": "false" | |
} | |
], | |
"fingerprint": "1561392779855", | |
"firingTriggerId": [ | |
"16" | |
], | |
"parentFolderId": "7", | |
"tagFiringOption": "ONCE_PER_EVENT", | |
"paused": true | |
} | |
], | |
"trigger": [ | |
{ | |
"accountId": "4701996609", | |
"containerId": "11992448", | |
"triggerId": "16", | |
"name": "Window Loaded", | |
"type": "WINDOW_LOADED", | |
"fingerprint": "1561392642942", | |
"parentFolderId": "7" | |
}, | |
{ | |
"accountId": "4701996609", | |
"containerId": "11992448", | |
"triggerId": "17", | |
"name": "WpForm Submits", | |
"type": "CUSTOM_EVENT", | |
"customEventFilter": [ | |
{ | |
"type": "EQUALS", | |
"parameter": [ | |
{ | |
"type": "TEMPLATE", | |
"key": "arg0", | |
"value": "{{_event}}" | |
}, | |
{ | |
"type": "TEMPLATE", | |
"key": "arg1", | |
"value": "wpFormSubmit" | |
} | |
] | |
} | |
], | |
"fingerprint": "1561387059054", | |
"parentFolderId": "7" | |
} | |
], | |
"variable": [ | |
{ | |
"accountId": "4701996609", | |
"containerId": "11992448", | |
"variableId": "14", | |
"name": "GA UTC", | |
"type": "gas", | |
"parameter": [ | |
{ | |
"type": "TEMPLATE", | |
"key": "cookieDomain", | |
"value": "auto" | |
}, | |
{ | |
"type": "BOOLEAN", | |
"key": "doubleClick", | |
"value": "false" | |
}, | |
{ | |
"type": "BOOLEAN", | |
"key": "setTrackerName", | |
"value": "false" | |
}, | |
{ | |
"type": "BOOLEAN", | |
"key": "useDebugVersion", | |
"value": "false" | |
}, | |
{ | |
"type": "BOOLEAN", | |
"key": "useHashAutoLink", | |
"value": "false" | |
}, | |
{ | |
"type": "BOOLEAN", | |
"key": "decorateFormsAutoLink", | |
"value": "false" | |
}, | |
{ | |
"type": "BOOLEAN", | |
"key": "enableLinkId", | |
"value": "false" | |
}, | |
{ | |
"type": "BOOLEAN", | |
"key": "enableEcommerce", | |
"value": "false" | |
}, | |
{ | |
"type": "TEMPLATE", | |
"key": "trackingId", | |
"value": "{{UTC Code}}" | |
} | |
], | |
"fingerprint": "1561387059057", | |
"parentFolderId": "15" | |
}, | |
{ | |
"accountId": "4701996609", | |
"containerId": "11992448", | |
"variableId": "13", | |
"name": "UTC Code", | |
"type": "c", | |
"parameter": [ | |
{ | |
"type": "TEMPLATE", | |
"key": "value", | |
"value": "UA-131102325-1" | |
} | |
], | |
"fingerprint": "1561387319029", | |
"parentFolderId": "15", | |
"formatValue": {} | |
}, | |
{ | |
"accountId": "4701996609", | |
"containerId": "11992448", | |
"variableId": "10", | |
"name": "wpFormAction", | |
"type": "jsm", | |
"parameter": [ | |
{ | |
"type": "TEMPLATE", | |
"key": "javascript", | |
"value": "function() {\n var formAction = {{wpFormElement}}.action;\n return formAction ? formAction : undefined;\n}" | |
} | |
], | |
"fingerprint": "1558633401730", | |
"parentFolderId": "7", | |
"formatValue": {} | |
}, | |
{ | |
"accountId": "4701996609", | |
"containerId": "11992448", | |
"variableId": "9", | |
"name": "wpFormElement", | |
"type": "v", | |
"parameter": [ | |
{ | |
"type": "INTEGER", | |
"key": "dataLayerVersion", | |
"value": "2" | |
}, | |
{ | |
"type": "BOOLEAN", | |
"key": "setDefaultValue", | |
"value": "false" | |
}, | |
{ | |
"type": "TEMPLATE", | |
"key": "name", | |
"value": "wpFormElement" | |
} | |
], | |
"fingerprint": "1558633401729", | |
"parentFolderId": "7", | |
"formatValue": {} | |
}, | |
{ | |
"accountId": "4701996609", | |
"containerId": "11992448", | |
"variableId": "11", | |
"name": "wpFormId", | |
"type": "jsm", | |
"parameter": [ | |
{ | |
"type": "TEMPLATE", | |
"key": "javascript", | |
"value": "function() {\n var formId = {{wpFormElement}}.id;\n return formId ? formId : undefined;\n}" | |
} | |
], | |
"fingerprint": "1558633401731", | |
"parentFolderId": "7", | |
"formatValue": {} | |
}, | |
{ | |
"accountId": "4701996609", | |
"containerId": "11992448", | |
"variableId": "12", | |
"name": "wpFormInput0", | |
"type": "jsm", | |
"parameter": [ | |
{ | |
"type": "TEMPLATE", | |
"key": "javascript", | |
"value": "function() {\n var field = {{wpFormElement}}.elements.item(0) //First element of form\n return field ? field.value : undefined;\n}" | |
} | |
], | |
"fingerprint": "1558633401732", | |
"parentFolderId": "7", | |
"formatValue": {} | |
} | |
], | |
"folder": [ | |
{ | |
"accountId": "4701996609", | |
"containerId": "11992448", | |
"folderId": "15", | |
"name": "GA Settings", | |
"fingerprint": "1561387059053" | |
}, | |
{ | |
"accountId": "4701996609", | |
"containerId": "11992448", | |
"folderId": "7", | |
"name": "wpForms Submits", | |
"fingerprint": "1558633401717" | |
} | |
], | |
"builtInVariable": [ | |
{ | |
"accountId": "4701996609", | |
"containerId": "11992448", | |
"type": "EVENT", | |
"name": "Event" | |
} | |
], | |
"fingerprint": "0", | |
"tagManagerUrl": "https://tagmanager.google.com/#/versions/accounts/4701996609/containers/11992448/versions/0?apiLink=version" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment