angular-ga is a very straightforward AngularJS adapter of the
new google analytics script.
It gives you full control of your analytics, exposing the google's ga() function for you.
What it means is, that it will set the page field on every route change for You, but you will
have to send the pageviews, events etc. manually. But on the other hand, you have the full control of that process.
Include the new universal analytics script in your html as usual, but remove ga('send', 'pageview');
angular.module('yourModule', ['ga'])angular-ga service is accessible as ga. Use it exactly the same, as ga() asynchronous function:
angular.module('myModule')
.controller('myCtrl', function (ga) {
ga('set', 'dimension1', 'Hello!');
ga('send', 'pageview', {title: 'Hello world!'});
});Contents of the directive should be the array of parameters for ga() function.
You can skip the enclosing array '[]' if you start with the single-quote character.
Of course, you can use angular expressions, as this is evaluated.
Both samples are equivalent to calling ga('send', 'event', 'player', 'play', video.id) on the click event:
<a href="#" ga="'send', 'event', 'player', 'play', video.id"></a>
<a href="#" ga="['send', 'event', 'player', 'play', video.id]"></a>You can call ga several times by passing an array of arrays:
<a href="#" ga="[['set', 'metric1', 10], ['send', 'event', 'player', 'play', video.id]]"></a>You can change the event by providing ga-on attribute:
<input type="text" ga="'send', 'event', 'focus'" ga-on="focus" />By using ga-on="init" you can call ga as soon as the html is parsed:
<div ga="'send', 'pageview', {title: 'Hello world!'}" ga-on="init" />If ga attribute is empty, the event is guesses from the context as follows:
category
link-outif href begins withhttp://link-inif href is anything else, except#buttonfor anything else
action
- value of
hrefattribute if present clickfor anything else
label
- value of
titleattribute if present - for
inputelements value ofvalueattribute if present - text contents for anything else
You can use attributes ga-category, ga-action, ga-label and ga-value to override
default behaviour.
