Created
June 3, 2022 18:28
-
-
Save interactiveRob/223832db829c35a1c768a94597dd8632 to your computer and use it in GitHub Desktop.
ES6 Gravity Forms GTM Custom Event
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
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