-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathga.js
71 lines (62 loc) · 2.47 KB
/
ga.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
(function (angular) {
'use strict';
angular.module('ga', [])
.factory('ga', ['$window', function ($window) {
var ga = function() {
if (angular.isArray(arguments[0])) {
for(var i = 0; i < arguments.length; ++i) {
ga.apply(this, arguments[i]);
}
return;
}
// console.log('ga', arguments);
if ($window.ga) {
$window.ga.apply(this, arguments);
}
};
return ga;
}])
.run(['$rootScope', '$location', 'ga', function ($rootScope, $location, ga) {
$rootScope.$on('$routeChangeStart', function() {
ga('set', 'page', $location.url());
});
}])
/**
ga="'send', 'event', 'test'" ga-on="click|hover|init"
*/
.directive('ga', ['ga', function(ga) {
return {
restrict: 'A',
scope: false,
link: function($scope, $element, $attrs) {
var bindToEvent = $attrs.gaOn || 'click';
var onEvent = function() {
var command = $attrs.ga;
if (command) {
if (command[0] === '\'') command = '[' + command + ']';
command = $scope.$eval(command);
} else {
// auto command
var href = $element.attr('href');
if (href && href === '#') href = '';
var category = $attrs.gaCategory ? $scope.$eval($attrs.gaCategory) :
(href && href[0] !== '#' ? (href.match(/\/\//) ? 'link-out' : 'link-in') : 'button'),
action = $attrs.gaAction ? $scope.$eval($attrs.gaAction) :
(href ? href : 'click'),
label = $attrs.gaLabel ? $scope.$eval($attrs.gaLabel) :
($element[0].title || ($element[0].tagName.match(/input/i) ? $element.attr('value') : $element.text())).substr(0, 64),
value = $attrs.gaValue ? $scope.$eval($attrs.gaValue) : null;
command = ['send', 'event', category, action, label];
if (value !== null) command.push(value);
}
ga.apply(null, command);
};
if (bindToEvent === 'init') {
onEvent();
} else {
$element.bind(bindToEvent, onEvent);
}
}
};
}]);
})(angular);