这是 GA Tracking 的详细说明,对 ST2+ 来说很简单。我已经介绍了如何初始化、设置基本导航跟踪以及包括自定义变量和事件跟踪在内的几种替代方法。
/*
Standard method to init GA tracking.
These can be fired from launch.
*/
initialiseGoogleAnalytics : function() {
//Add Google Analytics Key
window._gaq = window._gaq || [];
window._gaq.push(['_setAccount', MyApp.config.googleAnalytics.code]);
window._gaq.push(['_setDomainName', MyApp.config.googleAnalytics.domain]);
window._gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
},
/*
This method can be set to a globally accessable reference.
For instance:
MyApp.Util = {}; // reference for common utility functions
MyApp.Util.gaTrackEvent = function(data) {};
Thus allowing MyApp.Util.gaTrackEvent(data) from anywhere in app.
Alternatively, add as function to Application for
this.getApplication().gaTrackEvent(data);
*/
gaTrackEvent : function(data) {
//Push data to Google Analytics
// optional prefix for mobile devices - unnecessary for your interest
var prefix = 'mobile/';
// don't track homepage/default hits
if (data == 'home') {
//Ignore Home
return;
}
// basic tracking
_gaq.push(['_trackPageview', prefix + data]);
// OPTIONAL ALTERNATIVES FOR DETAILED TRACKING
// detailed tracking - condensed
_gaq.push(['_setCustomVar',
1, // custom variable slot
'customEventName', // custom variable name
'value1|value2|value3', // multiple values using 1 slot
2 // sets scope to session-level
]);
_gaq.push(['_trackEvent',
'ParentEventName',
'SomeValue'
]);
// detailed tracking - each variable using own slot
_gaq.push(['_setCustomVar',
1,
'stage1',
'value1',
2
]);
_gaq.push(['_setCustomVar',
2,
'stage2',
'value2',
2
]);
_gaq.push(['_setCustomVar',
3,
'stage3',
'value3',
2
]);
_gaq.push(['_trackEvent',
'ParentEventName',
'SomeValue'
]);
}
/*
Set up a controller to handle GA tracking.
This way you can keep the unique events you wish to track central,
while also handling default tracking and SEO.
For example, a controller for Registration might want tracking on success.
this.getRegistration().fireEvent('registrationsuccess');
*/
config: {
control: {
"navigationview": {
activeitemchange: 'generalSEOhandler'
},
"#registration": {
registrationsuccess: 'onRegistrationSuccess'
},
...
},
},
generalSEOhandler: function(container, value, oldValue, eOpts) {
if (value === 0) {
return false;
}
// ignoreDefaultSeo - boolean custom config that can be applied to views.
var ignoreDefaultSeo = value.getInitialConfig('ignoreDefaultSeo');
if (Ext.isDefined(ignoreDefaultSeo) && ignoreDefaultSeo == 1) {
// optional handler for special cases...
} else {
// Use default
var itemId = value.getItemId();
itemId = itemId.replace(/^ext-/,''); // Remove the prefix ext-
itemId = itemId.replace(/-[0-9]?$/,''); // Remove the suffix -1,-2...
// Alternatively use xtype of the view (my preference)
// This will require the xtypes of your views to match the main site pages.
var itemId = value.config.xtype;
this.trackEvent(itemId);
//console.log('USE DEFAULT', value.getId(), value.getItemId(), value);
}
},
onRegistrationSuccess: function(eventOptions) {
var app = this.getApplication(),
trackUrl;
trackUrl = 'new-member';
if (Ext.isDefined(app.accountReactivated) && app.accountReactivated == 1) {
trackUrl = 'reactivated-member';
}
if (Ext.isDefined(app.registeredUsingFacebook) && app.registeredUsingFacebook == 1) {
trackUrl += '/facebook';
} else {
trackUrl += '/non-facebook';
}
// console.log('onRegistrationSuccess', trackUrl);
this.trackEvent(trackUrl);
},
trackEvent: function(data) {
// if using MyApp.Util.gaTrackEvent() technique
MyApp.Util.gaTrackEvent(data);
// if gaTrackEvent() an application method
this.getApplication().gaTrackEvent(data);
}
}