1

How does one go about setting analytics up on an angulardart web application? I read Tracking Google Analytics Page Views with Angular.js but there isn't a $scope in angulardart. I'm using ng-view, so I'm guessing I need to somehow push a location to the ga variable, most likely in the RouteInitializer implementation? Maybe in the ngRoute section? I'm unsure. Thanks in advance.

EDIT:

JS:

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-XXXX', 'auto');
  ga('send', 'pageview');

  window.addEventListener('popstate', function(event) {
    ga('send', 'pageview');
  });
</script>

ROUTER:

void myRouteInitializer(Router router, RouteViewFactory views) {
  views.configure({
    'home': ngRoute(
        path: '/',
        enter: views('views/home.html')),
    'challenge': ngRoute(
        path: '/challenge/:challengeId',
        enter: views('views/view_challenge.html')),
    'completed': ngRoute(
        path: '/completed',
        enter: views('views/completed.html')),
    '404': ngRoute(
        defaultRoute: true,
        enter: (RouteEnterEvent e) => router.go('home', {}, replace: true))
});
Community
  • 1
  • 1
Squiggler
  • 496
  • 6
  • 14

1 Answers1

4

If your routing is being done with pushState, you should be able to just call

ga('send', 'pageview');

This will already read the correct URL shown in the address bar, even if it wasn't a real page load. So in theory, this should do everything for you:

window.addEventListener('popstate', function(event) {
  ga('send', 'pageview');
});
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Danny Tuppeny
  • 40,147
  • 24
  • 151
  • 275
  • Sadly this doesn't work. I've edited my question to include my router and js. Reading the link in your answer, I found this: `URL of the page being tracked. By default, analytics.js sets this to the full document URL, excluding the fragment identifier.` AngularDart uses the fragment. Not sure how it's supposed to work. – Squiggler Oct 02 '14 at 05:45
  • Yeah; that's what I meant by "If your routing is being done with pushState". The fragment is compatible with more browsers, but pushState is the new shiny thing that lets you have /nice/urls and would log with this code. – Danny Tuppeny Oct 02 '14 at 16:39
  • Ah I see. Seems as though I had pushState disabled in my main.dart. Enabling does not change my route anymore, which is why I guess I had it disabled to begin with. I'll accept your answer and make a new one as to maybe why it isn't working. – Squiggler Oct 03 '14 at 17:11
  • 1
    Returning to add that I went ahead and put `` in each of my views that I wanted pushed to google, and can confirm that my views are now being tracked WITHOUT needing pushState enabled. This is for wayward souls looking for an answer – Squiggler Oct 07 '14 at 18:38
  • Is this answer still valid for angularDart 7 in 2022? @Danny Tuppeny – Isaque Neves May 09 '22 at 14:14