Skip to content

Commit 3b5480e

Browse files
chore(doc-app): ensure only canonical paths get sent to Google Analytics
Before we were simply sending the current location, but multiple URLs map to the same document. Now, we use the canonical path of the current document if available and fall back to the $location path otherwise. Includes tests!! Closes angular#6402
1 parent a4078fc commit 3b5480e

File tree

3 files changed

+59
-24
lines changed

3 files changed

+59
-24
lines changed

docs/app/src/app.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
angular.module('docsApp', [
2+
'ngRoute',
3+
'ngCookies',
4+
'ngSanitize',
5+
'ngAnimate',
6+
'DocsController',
7+
'versionsData',
8+
'pagesData',
9+
'directives',
10+
'errors',
11+
'examples',
12+
'search',
13+
'tutorials',
14+
'versions',
15+
'bootstrap',
16+
'bootstrapPrettify',
17+
'ui.bootstrap.dropdown'
18+
])
19+
20+
21+
.config(function($locationProvider) {
22+
$locationProvider.html5Mode(true).hashPrefix('!');
23+
});

docs/app/src/docs.js

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,4 @@
1-
angular.module('docsApp', [
2-
'ngRoute',
3-
'ngCookies',
4-
'ngSanitize',
5-
'ngAnimate',
6-
'versionsData',
7-
'pagesData',
8-
'directives',
9-
'errors',
10-
'examples',
11-
'search',
12-
'tutorials',
13-
'versions',
14-
'bootstrap',
15-
'bootstrapPrettify',
16-
'ui.bootstrap.dropdown'
17-
])
18-
19-
20-
.config(function($locationProvider) {
21-
$locationProvider.html5Mode(true).hashPrefix('!');
22-
})
23-
1+
angular.module('DocsController', [])
242

253
.controller('DocsController', function($scope, $rootScope, $location, $window, $cookies, NG_PAGES, NG_NAVIGATION, NG_VERSION) {
264

@@ -52,7 +30,8 @@ angular.module('docsApp', [
5230
};
5331

5432
$scope.afterPartialLoaded = function() {
55-
$window._gaq.push(['_trackPageview', $location.path()]);
33+
var pagePath = $scope.currentPage ? $scope.currentPage.path : $location.path();
34+
$window._gaq.push(['_trackPageview', pagePath]);
5635
};
5736

5837
/** stores a cookie that is used by apache to decide which manifest ot send */

docs/app/test/docsSpec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
describe("DocsController", function() {
2+
var $scope;
3+
4+
angular.module('fake', [])
5+
.value('$cookies', {})
6+
.value('NG_PAGES', {})
7+
.value('NG_NAVIGATION', {})
8+
.value('NG_VERSION', {});
9+
10+
beforeEach(module('fake', 'DocsController'));
11+
beforeEach(inject(function($rootScope, $controller) {
12+
$scope = $rootScope;
13+
$controller('DocsController', { $scope: $scope });
14+
}));
15+
16+
17+
describe('afterPartialLoaded', function() {
18+
it("should update the Google Analytics with currentPage path if currentPage exists", inject(function($window) {
19+
$window._gaq = [];
20+
$scope.currentPage = { path: 'a/b/c' };
21+
$scope.afterPartialLoaded();
22+
expect($window._gaq.pop()).toEqual(['_trackPageview', 'a/b/c']);
23+
}));
24+
25+
26+
it("should update the Google Analytics with $location.path if currentPage is missing", inject(function($window, $location) {
27+
$window._gaq = [];
28+
spyOn($location, 'path').andReturn('x/y/z');
29+
$scope.afterPartialLoaded();
30+
expect($window._gaq.pop()).toEqual(['_trackPageview', 'x/y/z']);
31+
}));
32+
});
33+
});

0 commit comments

Comments
 (0)