The Ti.Google.Analytics project has been converted to use CommonJS. [1]
By using commonJS, all of our windows are in same context, we can simply call analytics.trackPageview directly inside windows.var gaModule = require('Ti.Google.Analytics'); var analytics = new gaModule('UA-XXXXXX-X'); // Call the next function if you want to reset the analytics to a new first time visit. // This is useful for development only and should not go into a production app. //analytics.reset(); // The analytics object functions must be called on app.js otherwise it will loose it's context Ti.App.addEventListener('analytics_trackPageview', function(e){analytics.trackPageview('/iPad' + e.pageUrl); }); Ti.App.addEventListener('analytics_trackEvent', function(e){ analytics.trackEvent(e.category, e.action, e.label, e.value); }); // Starts a new session as long as analytics.enabled = true // Function takes an integer which is the dispatch interval in seconds analytics.start(10,true); // You don't need to call stop on application close, but this is just to show you can callstop at any time (Basically sets enabled = false) Titanium.App.addEventListener('close', function(e){ analytics.stop(); });
However, for windows created by window.url property, the global variable "analytics" is not available. For compatible reason, in window created by "url" property, we can still fire a global event to track. Like this,
Ti.App.fireEvent("analytics_trackPageview", {pageUrl:'myhomepage'});
The old style Ti.Google.Analytics [2]
- /* analytics */
- Titanium.include('analytics.js');
- var analytics = new Analytics('UA-XXXXXX-XX');
- Titanium.App.addEventListener('analytics_trackPageview', function(e){
- var path = "/app/" + Titanium.Platform.name;
- analytics.trackPageview(path + e.pageUrl);
- });
- Titanium.App.addEventListener('analytics_trackEvent', function(e){
- analytics.trackEvent(e.category, e.action, e.label, e.value);
- });
- Titanium.App.Analytics = {
- trackPageview:function(pageUrl){
- Titanium.App.fireEvent('analytics_trackPageview', {pageUrl:pageUrl});
- },
- trackEvent:function(category, action, label, value){
- Titanium.App.fireEvent('analytics_trackEvent', {category:category, action:action, label:label, value:value});
- }
- }
- analytics.start(10);
- /* analytics end */
- // 传递参数:page名
- Titanium.App.Analytics.trackPageview('/hogePage');
- // 传递参数:分类名,动作名,标示,值
- Titanium.App.Analytics.trackEvent('Category','mogeAction','hoge',1);
For compatible reason, if you want to "include" the new GA in your old style window created by window.url property,
One trick to define a variable in app.js, like
var newGA = true;
And check if newGA is defined in Ti.Google.Analytics.js
if (typeof newGA != 'undefined'){
module.exports = commonJSWrapper;
} else {
Ti.API.warn('Google Analytics Used in Old Style Window');}
Then you can still use "include" in your old style window.
Titanium.include('Ti.Google.Analytics.js');var analytics = new Analytics('XX-XXXXXXXXX');
Reference
[1] https://github.com/rogchap/Titanium-Google-Analytics
[2] http://rensanning.iteye.com/blog/1315134
[1] https://github.com/rogchap/Titanium-Google-Analytics
[2] http://rensanning.iteye.com/blog/1315134
This comment has been removed by a blog administrator.
ReplyDelete