Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to ui-router ($state) #225

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 52 additions & 3 deletions dist/angular-google-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
offlineMode = false,
pageEvent = '$routeChangeSuccess',
readFromRoute = false,
readFromState = false,
removeRegExp,
testMode = false,
traceDebuggingMode = false,
Expand Down Expand Up @@ -199,11 +200,17 @@
return this;
};

// Enable reading page url from route object
// Enable reading page url from route object (ngRoute)
this.readFromRoute = function(val) {
readFromRoute = !!val;
return this;
};

// Enable reading page url from state object (ui-router)
this.readFromState = function(val) {
readFromState = !!val;
return this;
};

/**
* Public Service
Expand All @@ -214,7 +221,8 @@
'$rootScope',//
'$window', //
'$injector', // To access ngRoute module without declaring a fixed dependency
function ($document, $location, $log, $rootScope, $window, $injector) {
'$timeout', // To wait for the $state loading
function ($document, $location, $log, $rootScope, $window, $injector, $timeout) {
var that = this;

/**
Expand Down Expand Up @@ -245,13 +253,28 @@
$route = $injector.get('$route');
}
}

// Try to read state configuration and log warning if not possible
var $state = {};
if (readFromState) {
if (!$injector.has('$state')) {
$log.warn('$state service is not available. Make sure you have included ng-route in your application dependencies.');
} else {
$state = $injector.get('$state');
}
}

// Get url for current page
var getUrl = function () {
// Using ngRoute provided tracking urls
if (readFromRoute && $route.current && ('pageTrack' in $route.current)) {
return $route.current.pageTrack;
}

// Using ui-router provided tracking urls
if (readFromState && $state.current && ('pageTrack' in $state.current)) {
return $state.current.pageTrack;
}

// Otherwise go the old way
var url = trackUrlParams ? $location.url() : $location.path();
Expand Down Expand Up @@ -590,7 +613,19 @@
}

if (trackRoutes && !ignoreFirstPageLoad) {
_ga(generateCommandName('send', trackerObj), 'pageview', trackPrefix + getUrl());
if (readFromState) {
// Evaluate whether send or not the first pageview
// I didn't find a better approach than $timeout since in this context the $state hasn't been loaded yet
$timeout(function() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of the timeout is there an event you could listen to? Is the $stateChangeSuccess event sufficient for tracking the first route setup by ui-router? Could this first call simply not happen for ui-router as it will be caught by the event?

if ($state.current && !$state.current.doNotTrack) {
_ga(generateCommandName('send', trackerObj), 'pageview', trackPrefix + getUrl());
}
}, 1000);
}
else {
// Send immediately the pageview
_ga(generateCommandName('send', trackerObj), 'pageview', trackPrefix + getUrl());
}
}
});
//
Expand Down Expand Up @@ -1133,6 +1168,19 @@

that._trackPage();
});

$rootScope.$on('$stateChangeSuccess', function () {
// Apply $state based filtering if configured
if (readFromState) {
// Avoid tracking undefined routes, routes without template (e.g. redirect routes)
// and those explicitly marked as 'do not track'
if (!$state.current || !$state.current.templateUrl || $state.current.doNotTrack) {
return;
}
}

that._trackPage();
});
}

return {
Expand All @@ -1158,6 +1206,7 @@
logAllCalls: logAllCalls,
pageEvent: pageEvent,
readFromRoute: readFromRoute,
readFromState: readFromState,
removeRegExp: removeRegExp,
testMode: testMode,
traceDebuggingMode: traceDebuggingMode,
Expand Down