analytics.js 939 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. define([
  2. 'angular',
  3. 'jquery',
  4. 'app/core/core_module',
  5. 'app/core/config',
  6. ],
  7. function(angular, $, coreModule, config) {
  8. 'use strict';
  9. config = config.default;
  10. coreModule.default.service('googleAnalyticsSrv', function($rootScope, $location) {
  11. function gaInit() {
  12. $.getScript('https://www.google-analytics.com/analytics.js'); // jQuery shortcut
  13. var ga = window.ga = window.ga || function () { (ga.q = ga.q || []).push(arguments); }; ga.l = +new Date;
  14. ga('create', config.googleAnalyticsId, 'auto');
  15. return ga;
  16. }
  17. this.init = function() {
  18. $rootScope.$on('$viewContentLoaded', function() {
  19. var track = { page: $location.url() };
  20. var ga = window.ga || gaInit();
  21. ga('set', track);
  22. ga('send', 'pageview');
  23. });
  24. };
  25. }).run(function(googleAnalyticsSrv) {
  26. if (config.googleAnalyticsId) {
  27. googleAnalyticsSrv.init();
  28. }
  29. });
  30. });