Last active
May 3, 2019 21:08
-
-
Save fivetanley/873db6ba692d18df1b42463105797699 to your computer and use it in GitHub Desktop.
weird analytics in the routes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import {inject} from '@ember/service'; | |
export default Ember.Controller.extend({ | |
routerService: inject('router'), | |
analytics: inject('analytics'), | |
appName: 'Ember Twiddle', | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import EmberRouter from '@ember/routing/router'; | |
import config from './config/environment'; | |
const Router = EmberRouter.extend({ | |
location: 'none', | |
rootURL: config.rootURL | |
}); | |
Router.map(function() { | |
this.route('route1', {path: '/route1/:id'}); | |
this.route('route2', {path: '/route2/:id'}); | |
}); | |
export default Router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import { inject } from '@ember/service'; | |
import { getOwner } from '@ember/application'; | |
export default Ember.Route.extend({ | |
routerService: inject('router'), | |
analytics: inject('analytics'), | |
init() { | |
this._super(...arguments); | |
this.routerService.on('routeDidChange', (transition) => { | |
console.log('currentRoute', this.routerService.currentRoute); | |
if (!this.routerService.currentRoute) return; | |
const currentRoute = getOwner(this).lookup(`route:${this.routerService.currentRouteName}`); | |
console.log('currentRoute', currentRoute); | |
let eventName; | |
if (typeof currentRoute.analyticsEventName === 'function') { | |
eventName = currentRoute.analyticsEventName(currentRoute.currentModel, transition.to.params); | |
} else if (typeof currentRoute.analyticsEventName === 'string') { | |
eventName = currentRoute.analyticsEventName; | |
} | |
if (eventName) { | |
this.analytics.trackEvent(eventName); | |
} | |
}); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Route.extend({ | |
analyticsEventName(model, params) { | |
return 'route1' + params.id | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Route.extend({ | |
model() { | |
return { | |
id: 'butthead' | |
} | |
}, | |
analyticsEventName(model, params) { | |
return `route2 ${model.id}` | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Service.extend({ | |
tracks: Ember.computed(function() { | |
return Ember.A([]); | |
}), | |
trackEvent(eventName) { | |
this.tracks.pushObject(eventName) | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.15.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js", | |
"ember": "3.9.0", | |
"ember-template-compiler": "3.9.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment