Skip to content

Instantly share code, notes, and snippets.

@interactiveRob
Created June 3, 2022 18:28
Show Gist options
  • Save interactiveRob/223832db829c35a1c768a94597dd8632 to your computer and use it in GitHub Desktop.
Save interactiveRob/223832db829c35a1c768a94597dd8632 to your computer and use it in GitHub Desktop.
ES6 Gravity Forms GTM Custom Event
import { exists } from '@/helpers/utilities';
export let mixin = ({ node = null, config = {} } = {}) => {
if (!node) return false;
let state = {
node,
config,
};
let module = {
trackForm(event, formID) {
console.log(`tracking form ${formID}`);
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'formSubmission',
formID: formID,
});
},
setEventBindings() {
jQuery(document).on('gform_confirmation_loaded', this.trackForm);
},
init() {
if (!exists(state.node)) return;
this.setEventBindings();
},
};
return module.init();
};
export default {
init({ selector, config = {} }) {
let selected = document.querySelectorAll(selector);
if (!selected.length) return;
return [...selected].map((node, index) => {
return mixin({ node, config });
});
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment