0

I'm trying to set up the new google analytics tag for my angularjs website.

I basically added this to my index.html:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GOOGLE-ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'GOOGLE-ID'); //remove in order to avoid sending hits on index twice
</script>

This is my script.js:

var app = angular.module('app', ['plangular','ngRoute', 'ngSanitize', 'slick', 'chart.js', 'ngMeta']);

I added:

app.run(['ngMeta', '$rootScope' , '$location', '$window', function(ngMeta,$rootScope , $location, $window) { 
  ngMeta.init();


        $window.gtag('create', 'GOOGLE-ID', 'auto');


        $rootScope.$on('$stateChangeSuccess', function (event) {
            $window.gtag('send', 'pageview', $location.path());
        });

}]);

But I'm not able to see any hits on the analytics page, except for the index.

3 Answers 3

1

Have a look at https://github.com/angulartics/angulartics. It works well and has detailed installation instructions. You can even send events to multiple analytic services.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the tip, the gtag was not working but I switched to the old Google Analytics snippet and it's now working perfectly. I followed this doc: github.com/angulartics/angulartics-google-analytics/blob/master/…
0

In your index.html:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GOOGLE-ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'GOOGLE-ID', { 'send_page_view': false });
</script>

This will ensure that index doesn't count as a pageview when it loads. Then in your $stateChangeSuccess:

app.run(['ngMeta', '$rootScope', '$location', '$window', function(ngMeta, $rootScope , $location, $window) { 
  ngMeta.init();

  $rootScope.$on('$stateChangeSuccess', function (event) {
      $window.gtag('config', 'GOOGLE-ID', {'page_path': $location.path()});
      $window.gtag('event', 'page_view');
  });

}]);

Your code should now be firing pageviews for each state change. Gtag doesn't have create or send handlers. I think it only supports js, config, set, and event as the first param.

See https://developers.google.com/analytics/devguides/collection/gtagjs/single-page-applications for more info about using Gtag in SPAs.

3 Comments

This is not working, no routes are sent to analytics
Sorry! Edited answer to add 'page_view' event.
The 'event, 'page_view' is just a custom event, it's the line above that that sends the actual page view. Just a note for anyone looking at this
-1

I suggest embedding the Segment script into your index.html, extend analytics library onto the window object, and add tracking calls onto the (click) event handler:

@Component({
  selector: 'app-signup-btn',
  template: `
    <button (click)="trackEvent()">
      Signup with Segment today!
    </button>
  `
})
export class SignupButtonComponent {
  trackEvent() {
    window.analytics.track('User Signup');
  }
}

I’m the maintainer of https://github.com/segmentio/analytics-angular. I recommend checking it out if you want to solve this problem by using one singular API to manage your customer data, and be able to integrate into any other analytics tool (we support over 250+ destinations) - without writing any additional code. 🙂

1 Comment

the question was about AngularJS, not Angular (2+)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.