Having trouble installing google tag manager? Even worse, tracking pageviews not working because you are awesome enough to have a single page application? Hopefully this package helps you with this problem.
Similar to reywood:iron-router-ga
, this package allows you to provide your GTM ID via Meteor.settings
and fires virtual pageviews by sending messages to google tag manager's dataLayer
from iron router's onRun
hook for each route you have configured for tracking!
When dealing with a single page application, often you do not want a script available on all pages. This can be challenging, as navigating to new pages does not trigger a refresh of the dom and such, we cannot trigger google tag manager, to add/remove the tag for the current route. We have added a gtmBulldozer
to the window, such that when adding a custom script via google tag manager, you can declare to this package to wipe any scripts or variables from the dom/window.
Add the following to your Meteor.settings
{
"public": {
"gtm": {
"id": "GTM-XXXXXX"
}
}
}
Router.configure({
trackPageView: true
});
Router.configure({
trackPageView: true
});
Router.route("notMe", {
trackPageView: false
});
Router.route("justMe", {
trackPageView: true
});
Router.map(function() {
this.route("andMe", {
trackPageView: true
});
});
This is the part that connects the URL that this package sends to GTM to whichever tags you want!
This is where we name the variable coming from this package and give it a name that makes sense within your Google Tag Manager context. We have named the variable virtualPageURL
on our side.
Now kick your GTM into debug mode and test whether it's firing VirtualPageview on all the pages you expect!
Within your tag configuration, select 'more settings' and configure your 'fields to set', such that the location
variable is set with your virtualPageUrl
. We also export a variable virtualHostname
with the same event, such that you can filter or set up exceptions based on the hostname (e.g. filter out localhost).
To guarantee that we clear out page state when switching between routes, declare any javascript script ids or window scoped variables that you want gorillastack:iron-router-gtm
to clear for you before sending google tag manager a virtual pageview.
This example is for segment.io.