angular-google-analytics
This service lets you integrate google analytics tracker in your AngularJS applications easily.
You can use basic functions, Analytics.trackEvent('video', 'play', 'django.mp4');
or more advanced e-commerce features like product tracking, promo codes, transactions...
Proudly brought to you by @revolunet and @deltaepsilon.
features
- configurable
- automatic page tracking
- events tracking
- e-commerce tracking
- enhanced e-commerce tracking
- multiple-domains
- ga.js and and analytics.js support
- cross-domain support
- multiple tracking objects
install
bower install angular-google-analytics
Or alternatively, grab the dist/angular-google-analytics.min.js and include it in your project
example
var app = angular.module('app', ['angular-google-analytics'])
.config(function(AnalyticsProvider) {
AnalyticsProvider.setAccount('UA-XXXXX-xx');
AnalyticsProvider.trackPages(true);
AnalyticsProvider.trackUrlParams(true);
AnalyticsProvider.useDisplayFeatures(true);
AnalyticsProvider.trackPrefix('my-application');
AnalyticsProvider.useAnalytics(true);
AnalyticsProvider.useCrossDomainLinker(true);
AnalyticsProvider.setCrossLinkDomains(['domain-1.com', 'domain-2.com']);
AnalyticsProvider.ignoreFirstPageLoad(true);
AnalyticsProvider.useECommerce(true, false);
AnalyticsProvider.useECommerce(true, true);
AnalyticsProvider.useEnhancedLinkAttribution(true);
AnalyticsProvider.setExperimentId('12345');
AnalyticsProvider.setCookieConfig({
cookieDomain: 'foo.example.com',
cookieName: 'myNewName',
cookieExpires: 20000
});
AnalyticsProvider.setPageEvent('$stateChangeSuccess');
AnalyticsProvider.delayScriptTag(true);
}))
.run(function(Analytics) {
})
.controller('SampleController', function(Analytics) {
Analytics.trackPage('/video/detail/XXX');
Analytics.trackPage('/video/detail/XXX', 'Video XXX');
Analytics.trackPage('/video/detail/XXX', 'Video XXX', {dimension15: 'My Custom Dimension', metric18: 8000});
Analytics.trackEvent('video', 'play', 'django.mp4');
Analytics.trackEvent('video', 'play', 'django.mp4', 4);
Analytics.trackEvent('video', 'play', 'django.mp4', 4, true);
Analytics.trackEvent('video', 'play', 'django.mp4', 4, true, {dimension15: 'My Custom Dimension', metric18: 8000});
Analytics.addTrans('1', '', '2.42', '0.42', '0', 'Amsterdam', '', 'Netherlands', 'EUR');
Analytics.addItem('1', 'sku-1', 'Test product 1', 'Testing', '1', '1');
Analytics.addItem('1', 'sku-2', 'Test product 2', 'Testing', '1', '1');
Analytics.trackTrans();
Analytics.addImpression(productId, name, list, brand, category, variant, position, price);
Analytics.pageView();
Analytics.addImpression('sku-1', 'Test Product 1', 'Category List', 'Brand 1', 'Category-1', 'variant-1', '1', '24990');
Analytics.addImpression('sku-2', 'Test Product 2', 'Category List', 'Brand 2', 'Category-1', 'variant-3', '2', '2499');
Analytics.pageView();
Analytics.addProduct(productId, name, category, brand, variant, price, quantity, coupon, position);
Analytics.productClick(listName);
Analytics.addProduct('sku-2', 'Test Product 2', 'Category-1', 'Brand 2', 'variant-3', '2499', '1', 'FLAT10', '1');
Analytics.productClick('Search Result');
Analytics.addProduct(productId, name, category, brand, variant, price, quantity, coupon, position);
Analytics.trackDetail();
Analytics.addProduct('sku-2', 'Test Product 2', 'Category-1', 'Brand 2', 'variant-3', '2499', '1', 'FLAT10', '1');
Analytics.trackDetail();
Analytics.addProduct(productId, name, category, brand, variant, price, quantity, coupon, position);
Analytics.trackCart('add');
Analytics.addProduct('sku-2', 'Test Product 2', 'Category-1', 'Brand 2', 'variant-3', '2499', '1', 'FLAT10', '1');
Analytics.addProduct('sku-2', 'Test Product 2', 'Category-1', 'Brand 2', 'variant-3', '2499', '1', 'FLAT10', '1');
Analytics.trackCart('add');
Analytics.addProduct(productId, name, category, brand, variant, price, quantity, coupon, position);
Analytics.trackCart('remove');
Analytics.addProduct('sku-2', 'Test Product 2', 'Category-1', 'Brand 2', 'variant-3', '2499', '1', 'FLAT10', '1');
Analytics.addProduct('sku-2', 'Test Product 2', 'Category-1', 'Brand 2', 'variant-3', '2499', '1', 'FLAT10', '1');
Analytics.trackCart('remove');
Analytics.addProduct(productId, name, category, brand, variant, price, quantity, coupon, position);
Analytics.trackCheckout(checkoutStep, optionValue);
Analytics.addProduct('sku-2', 'Test Product 2', 'Category-1', 'Brand 2', 'variant-3', '2499', '1', 'FLAT10', '1');
Analytics.addProduct('sku-2', 'Test Product 2', 'Category-1', 'Brand 2', 'variant-3', '2499', '1', 'FLAT10', '1');
Analytics.trackCheckout(1, 'Visa');
Analytics.addProduct(productId, name, category, brand, variant, price, quantity, coupon, position);
Analytics.trackTransaction(transactionId, affiliation, revenue, tax, shipping, coupon, list, step, option);
Analytics.addProduct('sku-2', 'Test Product 2', 'Category-1', 'Brand 2', 'variant-3', '2222', '1', 'MEN10', '1');
Analytics.addProduct('sku-2', 'Test Product 2', 'Category-1', 'Brand 2', 'variant-3', '1111', '1', 'WOMEN10', '1');
Analytics.trackTransaction('T1234', 'Online Store - Web', '3333', '10', '200', 'FLAT10', '', '', '');
Analytics.addPromo(productId, name, creative, position);
Analytics.addPromo(productId, name, creative, position);
Analytics.addPromo(productId, name, creative, position);
Analytics.pageView();
Analytics.addPromo('PROMO_1234', 'Summer Sale', 'summer_banner2', 'banner_slot1');
Analytics.pageView();
Analytics.addPromo(promotionId, promotionName, creative, position);
Analytics.addPromo(promotionId, promotionName, creative, position);
Analytics.addPromo(promotionId, promotionName, creative, position);
Analytics.promoClick(promotionName);
Analytics.addPromo('PROMO_1234', 'Summer Sale', 'summer_banner2', 'banner_slot1');
Analytics.promoClick('Summer Sale');
Analytics.set('dimension1', 'Paid');
Analytics.createScriptTag({userId: 1234});
Analytics.createAnalyticsScriptTag({userId: 1234})
Analytics.trackTimings(timingCategory, timingVar, timingValue, timingLabel)
var endTime = new Date().getTime();
var timeSpent = endTime - startTime;
Analytics.trackTimings('Time to Checkout', 'User Timings', timeSpent);
});
directive
Alternatively you can use a directive to avoid filling controllers with Analytics.trackEvent()
statements. Note: the directive does not create an isolate scope.
<button type="button" ga-track-event="['video', 'play', 'django.mp4']"></button>
<!-- OR -->
<button type="button" ga-track-event="['video', 'play', 'django.mp4', 4, true, {dimension15: 'My Custom Dimension', metric18: 8000}]"></button>
You can define the properties on your controller too $scope.event = ['video', 'play', 'django.mp4']
and reference them
<button type="button" ga-track-event="event"></button>
configuration
AnalyticsProvider.setAccount('UA-XXXXX-xx');
AnalyticsProvider.trackPages(false);
AnalyticsProvider.setDomainName('XXX');
AnalyticsProvider.useDisplayFeatures(true);
AnalyticsProvider.useAnalytics(true);
AnalyticsProvider.ignoreFirstPageLoad(true);
AnalyticsProvider.useECommerce(true, false);
AnalyticsProvider.useECommerce(true, true);
AnalyticsProvider.useEnhancedLinkAttribution(true);
AnalyticsProvider.setExperimentId('12345');
AnalyticsProvider.setCookieConfig({
cookieDomain: 'foo.example.com',
cookieName: 'myNewName',
cookieExpires: 20000
});
AnalyticsProvider.setPageEvent('$stateChangeSuccess');
AnalyticsProvider.delayScriptTag(true);
AdBlock EasyPrivacy
AdBlock has a module named EasyPrivacy that is meant to block web tracking scripts. angular-google-analytics.js gets filtered out by the EasyPrivacy blacklist.
Users who are already concatenating and minifying their scripts should not notice a problem as long as the new script name is not also on the EasyPrivacy blacklist. Alternatively, consider changing the filename manually.
Licence
As AngularJS itself, this module is released under the permissive MIT license. Your contributions are always welcome.