analytics.js 911 B

123456789101112131415161718192021222324252627282930313233343536373839
  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. coreModule.default.service('googleAnalyticsSrv', function($rootScope, $location) {
  10. function gaInit() {
  11. $.getScript('https://www.google-analytics.com/analytics.js'); // jQuery shortcut
  12. var ga = window.ga = window.ga || function () { (ga.q = ga.q || []).push(arguments); }; ga.l = +new Date;
  13. ga('create', config.googleAnalyticsId, 'auto');
  14. return ga;
  15. }
  16. this.init = function() {
  17. $rootScope.$on('$viewContentLoaded', function() {
  18. var track = { page: $location.url() };
  19. var ga = window.ga || gaInit();
  20. ga('set', track);
  21. ga('send', 'pageview');
  22. });
  23. };
  24. }).run(function(googleAnalyticsSrv) {
  25. if (config.googleAnalyticsId) {
  26. googleAnalyticsSrv.init();
  27. }
  28. });
  29. });