Where would I put a piece of code that I want to execute on entry to every route?

#1

I want to log the user and current route every time a route is visited.

#2

I am sure there are other ways but we do ember-cli-analytics in router.js init

app/router.js

const Router = EmberRouter.extend( {

  init() {
    this._super(...arguments);
   // do your thing
  }

});
#3

Yeah there are a number of ways you could do it but I think @broerse’s suggestion of the router makes the most sense. I’d probably use the didTransition hook on the router personally (docs here).

1 Like
#4

Thanks guys never thought to use the router!