Счётчик от Google Analytics в приложении AngularJS

index.html

<script type="text/javascript">
	// TODO for mamba.ru only
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', 'UA-...']);
	_gaq.push(['_setDomainName', '...']);

	(function ()
	{
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s = document.getElementsByTagName('script')[0];
		s.parentNode.insertBefore(ga, s);
	})();
</script>

app.js

var app = angular.module('my', []).config();
app.run(['$location', function($location) {
	$rootScope.$on('$locationChangeSuccess', function(e, next, current) {
		trackPageView();
	});
	function trackPageView()
	{
		if (window._gaq)
		{
			// it's optional
			window._gaq.push(['_setCustomVar',
				1, // This custom var is set to slot #1.  Required parameter.
				'site_version', // The name acts as a kind of category for the user activity.  Required parameter.
				'my' // This value of the custom variable.  Required parameter.
			]);

			window._gaq.push(['_trackPageview'], $location.url());
		}
	}
}]);

https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables
http://davidwalsh.name/ajax-analytics

LEAVE A COMMENT