Recipe:

Adding Google Analytics tracking


Problem

You want to add analytics to your Ember application.

Solution

Subscribe to the didTransition event inside your application router.

In the following examples we’re using Google Analytics but it could be any other analytics product. Add google analytic’s base code to the html file that renders your ember app.

```html

My Ember Site

```

Then reopen the application router and add this function. It will be called when didTransition is fired by the router.

app/router.js

var Router = Ember.Router.extend({
  // customization goes here
});
Router.reopen({
  notifyGoogleAnalytics: Ember.on('didTransition', function() {
    return ga('send', 'pageview', {
        'page': this.get('url'),
        'title': this.get('url')
      });
  })
});
export default Router;

Discussion

The didTransition event is responsible for notifying listeners of any URL changes, in this example we are getting the path after the hash in the url so we can notify Google Analytics about moving between areas of the site.