Skip to content

Commit f38a63d

Browse files
committed
Refactor to move the metrics tracking to the ApplicationRoute as is now best practice
1 parent a40cf69 commit f38a63d

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

app/router.js

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,9 @@
11
import EmberRouter from '@ember/routing/router';
2-
import { scheduleOnce } from '@ember/runloop';
32
import config from 'ember-api-docs/config/environment';
4-
import { inject as service } from '@ember/service';
53

64
class AppRouter extends EmberRouter {
75
location = config.locationType;
86
rootURL = config.routerRootURL;
9-
10-
@service metrics;
11-
@service fastboot;
12-
13-
constructor() {
14-
super(...arguments);
15-
16-
if (!this.fastboot.isFastBoot) {
17-
this.on('routeDidChange', this, this._trackPage);
18-
}
19-
}
20-
21-
_trackPage() {
22-
scheduleOnce('afterRender', this, this.__trackPage);
23-
}
24-
25-
__trackPage() {
26-
// this is constant for this app and is only used to identify page views in the GA dashboard
27-
const hostname = config.APP.domain.replace(/(http|https)?:?\/\//g, '');
28-
29-
const page = this.url;
30-
const title =
31-
this.currentRouteName === undefined ? 'unknown' : this.currentRouteName;
32-
this.metrics.trackPage({ page, title, hostname });
33-
}
347
}
358

369
AppRouter.map(function () {

app/routes/application.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,31 @@ export default class ApplicationRoute extends Route {
1010
@service
1111
legacyModuleMappings;
1212

13+
@service
14+
router;
15+
16+
@service
17+
fastboot;
18+
19+
@service
20+
metrics;
21+
22+
constructor() {
23+
super(...arguments);
24+
if (!this.fastboot.isFastBoot) {
25+
this.router.on('routeDidChange', this.trackPage);
26+
}
27+
}
28+
29+
trackPage = () => {
30+
// this is constant for this app and is only used to identify page views in the GA dashboard
31+
const hostname = ENV.APP.domain.replace(/(http|https)?:?\/\//g, '');
32+
33+
const page = this.router.currentURL;
34+
const title = this.router.currentRouteName ?? 'unknown';
35+
this.metrics.trackPage({ page, title, hostname });
36+
};
37+
1338
async afterModel() {
1439
set(this, 'headData.cdnDomain', ENV.API_HOST);
1540
await this.legacyModuleMappings.initMappings();

0 commit comments

Comments
 (0)