Skip to content

Instantly share code, notes, and snippets.

@samikeijonen
Created May 22, 2023 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samikeijonen/1d5766642196736f33c88f5af30b8a70 to your computer and use it in GitHub Desktop.
Save samikeijonen/1d5766642196736f33c88f5af30b8a70 to your computer and use it in GitHub Desktop.
/**
* 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