Google Analytics wrapper for Angular
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
</script>
<script src="ng-google-analytics.min.js"></script>
Check official docs for up-to-date snippet
angular.module 'yourApp', ['vtex.ga']
.config (gaConfig) ->
gaConfig.trackingId = 'XX-XXXXXXXX-X'
gaConfig.appId = 'your_app_ID'
gaConfig.userId = 'current_user_ID'
Disable auto tracking of virtual pageviews:
.config (gaConfig) -> gaConfig.enableVirtualPageviews = false
Auto tracking of $http errors with gaInterceptor
.config ($httpProvider) -> $httpProvider.interceptors.push 'gaInterceptor'
Defaults and shortcuts:
ga-event
ga-category
: "button"ga-action
: "click" ( serves as trigger ifga-on
isn't specified. )ga-label
: element'slabel
,name
orid
attributesga-on
:ga-action
'sattrs.value
or "click" ( this is what fires events to GA, e.g: "hover" )
An example taking advantage of defaults:
<button id="sign-up" class="btn btn-primary" ga-event>Sign Up</button>
Full usage:
<button class="btn btn-primary"
ga-event ga-category="button" ga-action="click"
ga-label="sign-up" ga-on="click">Sign Up</button>
- Object containing event data:
category
,action
,label
,value
- Optional
metadata
Object as well value
must be anumber
, not required (check Universal Analytics docs)
e.g.:
eventData =
category: 'button'
action: 'click'
label: 'Sign Up'
value: 10
page
is a string, e.g.:location.href
, default: (location.pathname
+location.hash
)
- Shortcut to
@trackEvent
category: 'button'
action: 'click'
label: `label`
value: `value`
- v2.0.0:
gaService.trackEvent
now expects Object instead of Array for better validation of properties values and order before dispatching everything to GA. Refactors code and README in order to optimize usage for new Universal Analytics.
Inside src
you can find this module source code, written in CoffeeScript. To build the .js
and uglify it, install npm dev-dependencies and run grunt:
(sudo) npm i
grunt