Skip to content

Commit

Permalink
Merge branch 'SierraGolf-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
em0ney committed Oct 20, 2015
2 parents ac77cf5 + 546cd17 commit 93bc4e5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 44 deletions.
56 changes: 13 additions & 43 deletions lib/client/router.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,22 @@
var _super = {
route: Router.route
};

Router.route = function(name, options) {
options = attachAnalyticsOptions.call(this, options);
return _super.route.call(this, name, options);
};

var attachAnalyticsOptions = function(options) {
options = options || {};

if (shouldSendVirtualPageview.call(this, options)) {
attachVirtualPageviewSender.call(this, options);
Router.onRun(function () {
if (shouldSendVirtualPageview(Router.options, this.route.options)) {
gtmBulldozer.bulldoze();
dataLayer.push({
event: 'VirtualPageview',
virtualPageURL: this.url
});
}
this.next();
});

return options;
};

var shouldSendVirtualPageview = function(options) {
function shouldSendVirtualPageview(routerOptions, routeOptions) {
if (typeof (window.dataLayer) === 'undefined') {
return false;
}

if (options && typeof options.trackPageView !== 'undefined') {
return !!options.trackPageView;
if (routeOptions && typeof routeOptions.trackPageView !== 'undefined') {
return !!routeOptions.trackPageView;
}

return !!this.options && !!this.options.trackPageView;
};

var attachVirtualPageviewSender = function(options) {
var originalOnRun = options.onRun;

options.onRun = function() {
gtmBulldozer.bulldoze();
dataLayer.push({
event: 'VirtualPageview',
virtualPageURL: arguments[0].url
});

return callEventHandlerOrNext.call(this, originalOnRun, arguments);
};
};

var callEventHandlerOrNext = function(handler, args) {
if (handler) {
return handler.apply(this, [].slice.apply(args));
} else {
this.next();
}
return !!routerOptions && !!routerOptions.trackPageView;
};
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package.describe({
name: 'gorillastack:iron-router-gtm',
version: '0.1.2',
version: '0.2.0',
summary: 'Install google tag manager and track pageviews in iron-router',
git: '[email protected]:GorillaStack/meteor-iron-router-gtm.git',
documentation: 'README.md'
Expand Down

0 comments on commit 93bc4e5

Please sign in to comment.