Last active
August 15, 2024 15:15
-
-
Save pwfisher/5fd09ec2ccab253008f9 to your computer and use it in GitHub Desktop.
Ember.js Google Tag Manager utility
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
// Google Tag Manager | |
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': | |
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], | |
j=d.createElement(s);j.async=true;j.src= | |
'//www.googletagmanager.com/gtm.js?id='+i;f.parentNode.insertBefore(j,f); | |
})(window,document,'script','dataLayer','GTM-ABCDEF'); | |
// End Google Tag Manager | |
App.GtmUtil = { | |
defaultEvent: { | |
event: 'trackEvent', | |
eventCategory: '', | |
eventAction: '', | |
eventLabel: '', | |
eventValue: '', | |
}, | |
defaultPageView: { event: 'vpv', virtualPagePath: '' }, | |
createPayload: function (type, data) { | |
const defaultPayload = type === 'event' ? defaultEvent : defaultPageView | |
if (!data) return defaultPayload | |
const payload = {} | |
Object.keys(defaultPayload).forEach(key => { | |
payload[key] = data.hasOwnProperty(key) ? data[key] : defaultPayload[key]; | |
}) | |
return payload | |
}, | |
trackEvent: function (o) { | |
window.dataLayer.push(this.createPayload('Event', o)) | |
}, | |
trackPageView: function (path) { | |
window.dataLayer.push(this.createPayload('PageView', { virtualPagePath: path })) | |
} | |
} |
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
require('../utils/gtm-util') | |
Ember.Application.initializer({ | |
name: 'googleTagManager', | |
initialize: function (container, application) { | |
const router = container.lookup('router:main') | |
router.on('didTransition', function () { | |
App.GtmUtil.trackPageView(this.get('url')) | |
}) | |
} | |
}) |
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
// Sample usage of App.GtmUtil.trackEvent | |
require('../utils/gtm-util') | |
App.ApplicationRoute = Ember.Route.extend({ | |
actions: { | |
openModal: function (name, model) { | |
App.GtmUtil.trackEvent({ eventCategory: 'Application', eventAction: 'openModal', eventLabel: name }) | |
this.controllerFor(`modal.${name}`).set('model', model) | |
return this.render(`modal/${name}`, { | |
into: 'application', | |
outlet: 'modal' | |
}) | |
}, | |
closeModal: function () { | |
App.GtmUtil.trackEvent({ eventCategory: 'Application', eventAction: 'closeModal' }) | |
return this.disconnectOutlet({ | |
outlet: 'modal', | |
parentView: 'application' | |
}) | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this helped me a lot, thanks!