Skip to content

Commit e3db95b

Browse files
committed
Handle title="" and ga-* event overrides
1 parent 684eaf3 commit e3db95b

File tree

4 files changed

+40
-33
lines changed

4 files changed

+40
-33
lines changed

README.md

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,38 +69,28 @@ By using `ga-on="init"` you can call `ga` as soon as the html is parsed:
6969

7070
## Use ga directive's auto events
7171

72-
If `ga` attribute is empty, the event is guesses from the context. Following examples
73-
are equivalent:
72+
If `ga` attribute is empty, the event is guesses from the context as follows:
7473

75-
```html
76-
<div ga>LABEL</div>
77-
<div ga="'send', 'event', 'button', 'click', 'LABEL'">LABEL</div>
78-
```
74+
**category**
7975

80-
```html
81-
<a href="#" ga>LABEL</a>
82-
<a href="#" ga="'send', 'event', 'button', 'click', 'LABEL'">LABEL</a>
83-
```
76+
- `link-out` if href begins with `http://`
77+
- `link-in` if href is anything else, except `#`
78+
- `button` for anything else
8479

85-
```html
86-
<a href="#anchor" ga>LABEL</a>
87-
<a href="#anchor" ga="'send', 'event', 'button', '#anchor', 'LABEL'">LABEL</a>
88-
```
80+
**action**
8981

90-
```html
91-
<a href="/" ga>LABEL</a>
92-
<a href="/" ga="'send', 'event', 'link-in', '/', 'LABEL'">LABEL</a>
93-
```
82+
- value of `href` attribute if present
83+
- `click` for anything else
9484

95-
```html
96-
<a href="https://github.com/panrafal/angular-ga" ga>LABEL</a>
97-
<a href="https://github.com/panrafal/angular-ga" ga="'send', 'event', 'link-out', 'https://github.com/panrafal/angular-ga', 'LABEL'">LABEL</a>
98-
```
85+
**label**
86+
87+
- value of `title` attribute if present
88+
- for `input` elements value of `value` attribute if present
89+
- text contents for anything else
90+
91+
You can use attributes `ga-category`, `ga-action`, `ga-label` and `ga-value` to override
92+
default behaviour.
9993

100-
```html
101-
<input type="submit" value="SUBMIT" ga />
102-
<input type="submit" value="SUBMIT" ga="'send', 'event', 'button', 'click', 'SUBMIT'" />
103-
```
10494

10595

10696
[![githalytics.com alpha](https://cruel-carlota.pagodabox.com/1b31cde4eb48524cf5194d3c2bf1ef68 "githalytics.com")](http://githalytics.com/panrafal/angular-ga)

ga.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ angular.module('ga', [])
1111
}
1212
return;
1313
}
14+
console.log('ga', arguments);
1415
if ($window.ga) {
1516
$window.ga.apply(this, arguments);
1617
}
17-
}
18+
};
1819

1920
return ga;
2021
}])
@@ -45,13 +46,17 @@ angular.module('ga', [])
4546
// auto command
4647
var href = $element.attr('href');
4748
if (href && href === '#') href = '';
48-
var category = href && href[0] !== '#' ? (href.match(/\/\//) ? 'link-out' : 'link-in') : 'button',
49-
action = href ? href : 'click',
50-
label = ($element[0].tagName.match(/input/i) ? $element.attr('value') : $element.text()).substr(0, 64);
51-
command = ['send', 'event', category, action, label];
49+
var category = $attrs.gaCategory ? $scope.$eval($attrs.gaCategory) :
50+
(href && href[0] !== '#' ? (href.match(/\/\//) ? 'link-out' : 'link-in') : 'button'),
51+
action = $attrs.gaAction ? $scope.$eval($attrs.gaAction) :
52+
(href ? href : 'click'),
53+
label = $attrs.gaLabel ? $scope.$eval($attrs.gaLabel) :
54+
($element[0].title || ($element[0].tagName.match(/input/i) ? $element.attr('value') : $element.text())).substr(0, 64),
55+
value = $attrs.gaValue ? $scope.$eval($attrs.gaValue) : 0;
56+
command = ['send', 'event', category, action, label, value];
5257
}
5358
ga.apply(null, command);
54-
}
59+
};
5560

5661
if (bindToEvent === 'init') {
5762
onEvent();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "angular-ga",
33
"description": "Google Universal Analytics adapter for AngularJS",
4-
"version": "0.1.2",
4+
"version": "0.1.3",
55
"author": "Rafal Lindemann <rl@stamina.pl>",
66
"main": "ga.js",
77
"repository": {

test/spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ describe('ga', function () {
8383
expect(window.ga).toHaveBeenCalledWith('send', 'event', 'button', '#anchor', 'Label');
8484
})
8585

86+
it('should have title', function() {
87+
el = compileElement('<a href="#anchor" title="Title" ga>Label</a>').triggerHandler('click')
88+
expect(window.ga).toHaveBeenCalledWith('send', 'event', 'button', '#anchor', 'Title');
89+
})
90+
8691
it('should click link', function() {
8792
el = compileElement('<a href="/" ga>Label</a>').triggerHandler('click')
8893
expect(window.ga).toHaveBeenCalledWith('send', 'event', 'link-in', '/', 'Label');
@@ -98,6 +103,13 @@ describe('ga', function () {
98103
expect(window.ga).toHaveBeenCalledWith('send', 'event', 'button', 'click', 'Submit');
99104
})
100105

106+
it('should have special attributes', function() {
107+
el = compileElement('<a href="#" title="Title" ga ga-category="cat" ga-action="act" ga-label="lab" ga-value="1">Label</a>').triggerHandler('click')
108+
expect(window.ga).toHaveBeenCalledWith('send', 'event', 'cat', 'act', 'lab', 1);
109+
})
110+
111+
112+
101113
});
102114

103115

0 commit comments

Comments
 (0)