Created
May 22, 2023 17:15
-
-
Save samikeijonen/1d5766642196736f33c88f5af30b8a70 to your computer and use it in GitHub Desktop.
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
/** | |
* Set dataLayer for Piwik Pro. | |
*/ | |
function dataLayer() { | |
window.dataLayer = window.dataLayer || []; | |
// Form field have been added as hidden field in the Gravity Form. | |
// It has class `js-datalayer-name`. | |
const formNameField = document.querySelector('.js-datalayer-name input'); | |
if (formNameField) { | |
window.dataLayer.push({ | |
event: 'submitForm', | |
// Value have been added as hidden field in the form. | |
category: formNameField.value | |
? formNameField.value | |
: 'No category detected', | |
action: 'submitForm', | |
label: window.location.href, | |
}); | |
} | |
// Footer form is in every page. | |
// In Gravity form field that has class `js-datalayer-name-footer`. | |
const formNameFieldFooter = document.querySelector( | |
'.js-datalayer-name-footer input' | |
); | |
if (formNameFieldFooter) { | |
window.dataLayer.push({ | |
event: 'newsletterSignupFooter', | |
// Value have been added as hidden field in the form. | |
category: formNameFieldFooter.value | |
? formNameFieldFooter.value | |
: 'No category detected', | |
action: 'newsletterSignup', | |
label: window.location.href, | |
}); | |
} | |
} | |
export default dataLayer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment