Add Discourse-Track-View header to XHR when transitioning

This commit is contained in:
Robin Ward 2015-02-05 16:07:51 -05:00
parent 4e64d16a47
commit a852f6c56f
3 changed files with 28 additions and 2 deletions

View File

@ -5,7 +5,14 @@ export default {
name: "page-tracking",
after: 'register-discourse-location',
initialize: function() {
initialize: function(container) {
// Tell our AJAX system to track a page transition
var router = container.lookup('router:main');
router.on('willTransition', function() {
Discourse.viewTrackingRequired();
});
var pageTracker = Discourse.PageTracker.current();
pageTracker.start();
@ -19,7 +26,6 @@ export default {
return;
}
// Also use Universal Analytics if it is present
if (typeof window.ga !== 'undefined') {
pageTracker.on('change', function(url, title) {

View File

@ -7,8 +7,15 @@
@namespace Discourse
@module Discourse
**/
var _trackView = false;
Discourse.Ajax = Em.Mixin.create({
viewTrackingRequired: function() {
_trackView = true;
},
/**
Our own $.ajax method. Makes sure the .then method executes in an Ember runloop
for performance reasons. Also automatically adjusts the URL to support installs
@ -42,6 +49,11 @@ Discourse.Ajax = Em.Mixin.create({
var performAjax = function(resolve, reject) {
if (_trackView) {
_trackView = false;
args.headers = { 'Discourse-Track-View': true };
}
args.success = function(xhr) {
Ember.run(null, resolve, xhr);
};

View File

@ -26,6 +26,14 @@ Discourse.Route = Ember.Route.extend({
},
actions: {
// Ember doesn't provider a router `willTransition` event so let's make one
willTransition: function() {
var router = this.container.lookup('router:main');
Ember.run.once(router, router.trigger, 'willTransition');
return this._super();
},
_collectTitleTokens: function(tokens) {
// If there's a title token method, call it and get the token
if (this.titleToken) {