<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxx-1']);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_trackPageview','android/home']);
(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);
})();
function bodyLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
cordova.addConstructor(function() {
cordova.addPlugin('analytics', new Analytics());
},false);
window.plugins.analytics.start("YOUR-ANALYTICS-ID", function(){console.log("Analytics: start success");}, function(){console.log("Analytics: start failure");})
}
////////////////
--
You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
For more info on PhoneGap or to download the code go to www.phonegap.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
For more info on PhoneGap or to download the code go to www.phonegap.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
For more info on PhoneGap or to download the code go to www.phonegap.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
For more info on PhoneGap or to download the code go to www.phonegap.com
I'm having a bit of a hard time getting this to work...I followed the instructions but nothing is being sent. So I dug a little deeper and it turns out that google analytics needs cookies to be enabled.This is straight from stack overflow:"cookies are proprietary to HTTP, and when you run something from file://, you're not using the HTTP protocol.Since you're not able to set cookies, ga.js refuses to send the _utm.gif request to Google's servers. No cookies get set; no request is sent to google, so nothing is logged in GA."
In my case nothing is being sent and I'm wondering if the cookies have something to do with it. Has anyone else had this problem or has it worked for the ones that tried...
var googleAnalytics = window.plugins.googleAnalyticsPlugin;
googleAnalytics.startTrackerWithAccountID("UA-MyCode");
googleAnalytics.trackPageview('/MobileiOS');
Which is working and I see in Analytics.function onLoad(){
document.addEventListener("deviceready", onDeviceReady, false);
}
Thanks for any help.
I have not been able to get this to build without link errors. Can you provide your plugins folder?
On Monday, 29 October 2012 22:18:34 UTC, nocksf wrote:I had been running with PG 1.7 and an older version of the plugin - where http://stackoverflow.com/questions/8097015/step-by-set-to-get-google-analytics-working-in-phonegap-1-2-0-on-ios-phonegapal helped a lot. From that starting point, here's what I just did to get it running on PG2.1, latest Google SDK, and xcode/ios. Sorry, it's not completely comprehensive, but it might help you down the path if you had it working with an older version.Got it basically working with PG 2.1.- downloaded the current SDK from Google (note there doesn't seem to be support for CustomVariables anymore).- put the header files (5) from the Library dir in a plugins folder- changed GoogleAnalyticsPlugin.h to this:#import <Foundation/Foundation.h>
#import "GAI.h"
#import <Cordova/CDV.h>
@interface GoogleAnalyticsPlugin : CDVPlugin {
}- (void) trackerWithTrackingId:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
- (void) trackEventWithCategory:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
- (void) trackView:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;@end
Changed GoogleAnalyticsPlugin.m to this:#import "GoogleAnalyticsPlugin.h"
// Dispatch period in seconds
static const NSInteger kGANDispatchPeriodSec = 2;
@implementation GoogleAnalyticsPlugin
- (void) trackerWithTrackingId:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
NSString* accountId = [arguments objectAtIndex:0];
[GAI sharedInstance].debug = YES;
[GAI sharedInstance].dispatchInterval = kGANDispatchPeriodSec;
[GAI sharedInstance].trackUncaughtExceptions = YES;
[[GAI sharedInstance] trackerWithTrackingId:accountId];
}
- (void) trackEventWithCategory:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
NSString* category = [options valueForKey:@"category"];
NSString* action = [options valueForKey:@"action"];
NSString* label = [options valueForKey:@"label"];
NSNumber* value = [options valueForKey:@"value"];
if (![[GAI sharedInstance].defaultTracker trackEventWithCategory:category
withAction:action
withLabel:label
withValue:value]) {
// Handle error here
NSLog(@"GoogleAnalyticsPlugin.trackEvent Error::");}
NSLog(@"GoogleAnalyticsPlugin.trackEvent::%@, %@, %@, %@",category,action,label,value);
}- (void) trackView:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
NSString* pageUri = [arguments objectAtIndex:0];
if (![[GAI sharedInstance].defaultTracker trackView:pageUri]) {
// TODO: Handle error here
}
}- (void) hitDispatched:(NSString *)hitString
{
NSString* callback = [NSString stringWithFormat:@"window.plugins.googleAnalyticsPlugin.hitDispatched(%d);", hitString];
[ self.webView stringByEvaluatingJavaScriptFromString:callback];
}
@endChanged GoogleAnalyticsPlugin.js to this (some of this is just a style preference from the original):if (!window.GA) {
window.GA = {
trackerWithTrackingId: function(id) {
PhoneGap.exec("GoogleAnalyticsPlugin.trackerWithTrackingId",id);
},
trackView: function(pageUri) {
PhoneGap.exec("GoogleAnalyticsPlugin.trackView",pageUri);
},
trackEventWithCategory: function(category,action,label,value) {
var options = {category:category,
action:action,
label:label,
value:value};
PhoneGap.exec("GoogleAnalyticsPlugin.trackEventWithCategory",options);
},
hitDispatched: function(hitString) {
//console.log("hitDispatched :: " + hitString);
},
trackerDispatchDidComplete: function(count) {
//console.log("trackerDispatchDidComplete :: " + count);
}
}}
then called my new js functions from my app. I had to create a new property in Analytics (I was using our existing id for web analytics before), but I can now see my mobile analytics on the real-time GA page.
- now you can use:
window.plugins.analytics.trackPageView("page1.html", function(){alert("Track: success");}, function(){alert("Track: failure");});
- window.plugins.analytics.trackPageView("category", "action", "event", 1, function(){alert("Track: success");}, function(){alert("Track: failure");});
Code for the bottom of your <head> section (maje sure you put it in script tags)function bodyLoad() {
document.addEventListener("deviceready", onDeviceReady, false);}
function onDeviceReady() {
cordova.addConstructor(function() {cordova.addPlugin('analytics', new Analytics());
},false);
I've only tried this in iOS, but here's how I got it working *without* using a plug-in.Essentially, Google Analytics won't work if its being used from a file:/// url. In iOS/PhoneGap this is the case. In order to solve this problem you must first download the ga.js file from google and include it as part of your local build. You'll notice the this file is obfuscated. Search the file for the string "file:" which should occur only once. When you find it, add an underscore to the beginning (so it becomes "_file:"). This prevents it matching the protocol of the page location (which is "file:").Now include the modified file in your program rather than the remote ga.js file and you'll find that analytics information will start to appear (check the Realtime display - very helpful of debugging).
Let me know if it works on Android.
@Alex,
I had the same issue as you. What fixed it for me was discovering that Google Analytics SDK 2.0+ uses different frameworks then the previous versions. Be sure to include the following.You should also be able to remove
CoreData.framework
SystemConfiguration.framework
libsqlite3.0.dylib unless another
part of your app requires it.
I am also having the same problem now. Everything seems to work with no errors but analytics are never registered. With previous versions I could see in real time my apps analytics.
SBCERAApp[3681:15e03] GoogleAnalytics 2.0b3 -[GAIDispatcher internalCreateTimer] (GAIDispatcher.m:195) DEBUG: Created timer to fire every 2.0s
2013-02-05 15:02:11.871 SBCERAApp[3681:15e03] Hit URL: http://www.google-analytics.com/collect
2013-02-05 15:02:11.872 SBCERAApp[3681:15e03] Method: POST
2013-02-05 15:02:11.872 SBCERAApp[3681:15e03] Header User-Agent: GoogleAnalytics/2.0b3 (iPhone Simulator; U; CPU iPhone OS 6.1 like Mac OS X; en-us)
2013-02-05 15:02:11.872 SBCERAApp[3681:15e03] Header Content-Length: 179
2013-02-05 15:02:11.873 SBCERAApp[3681:15e03] Header Content-Type: application/x-www-form-urlencoded; charset=UTF-8
2013-02-05 15:02:11.873 SBCERAApp[3681:15e03] Body: ul=en&an=SBCERAApp&_v=mi1b3&cid=2b423497187b5924ff1b5802d640e2a2&sc=start&t=appview&sd=24-bit&sr=320x480&cd=%2FMobileiOS&tid=UA-26017499-1&v=1&av=1.0&qt=2018&z=3432372163228806075
2013-02-05 15:02:11.964 SBCERAApp[3681:15e03] GoogleAnalytics 2.0b3 -[GAIDispatcher dispatchComplete:withStartTime:withRetryNumber:withResponse:withData:withError:] (GAIDispatcher.m:392) DEBUG: Hit /GAIHit/p20 dispatched (90ms): HTTP status 200
2013-02-05 15:02:11.968 SBCERAApp[3681:15e03] GoogleAnalytics 2.0b3 -[GAIDispatcher dispatchComplete:withStartTime:withRetryNumber:withResponse:withData:withError:] (GAIDispatcher.m:413) DEBUG: Successfully dispatched hit /GAIHit/p20 (0 retries).
2013-02-05 15:02:11.969 SBCERAApp[3681:15e03] Hit URL: http://www.google-analytics.com/collect
2013-02-05 15:02:11.969 SBCERAApp[3681:15e03] Method: POST
2013-02-05 15:02:11.969 SBCERAApp[3681:15e03] Header User-Agent: GoogleAnalytics/2.0b3 (iPhone Simulator; U; CPU iPhone OS 6.1 like Mac OS X; en-us)
2013-02-05 15:02:11.970 SBCERAApp[3681:15e03] Header Content-Length: 172
2013-02-05 15:02:11.970 SBCERAApp[3681:15e03] Header Content-Type: application/x-www-form-urlencoded; charset=UTF-8
2013-02-05 15:02:11.970 SBCERAApp[3681:15e03] Body: ul=en&an=SBCERAApp&_v=mi1b3&cid=2b423497187b5924ff1b5802d640e2a2&t=appview&sd=24-bit&sr=320x480&cd=HTTabMobileiOS&tid=UA-26017499-1&v=1&av=1.0&qt=2098&z=3432372163228806076
2013-02-05 15:02:12.020 SBCERAApp[3681:15e03] GoogleAnalytics 2.0b3 -[GAIDispatcher dispatchComplete:withStartTime:withRetryNumber:withResponse:withData:withError:] (GAIDispatcher.m:392) DEBUG: Hit /GAIHit/p21 dispatched (49ms): HTTP status 200
2013-02-05 15:02:12.022 SBCERAApp[3681:15e03] GoogleAnalytics 2.0b3 -[GAIDispatcher dispatchComplete:withStartTime:withRetryNumber:withResponse:withData:withError:] (GAIDispatcher.m:413) DEBUG: Successfully dispatched hit /GAIHit/p21 (0 retries).
2013-02-05 15:02:12.022 SBCERAApp[3681:15e03] GoogleAnalytics 2.0b3 -[GAIDispatcher dispatchComplete:withStartTime:withRetryNumber:withResponse:withData:withError:] (GAIDispatcher.m:420) DEBUG: Pending hit queue drained.
2013-02-05 15:02:12.023 SBCERAApp[3681:15e03] GoogleAnalytics 2.0b3 -[GAIDispatcher cancelTimer] (GAIDispatcher.m:224) DEBUG: Canceled timer with interval 2.0s
I did all the steps, copied google analytics folder into plugins, added key to plist, replaced PhoneGap.exec with cordova.exec but still getting error plugin not found or not part of CVDplugin....please help its really urgent...using cordova 2.2.0--
-- You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
For more info on PhoneGap or to download the code go to www.phonegap.com
You received this message because you are subscribed to the Google Groups "phonegap" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phonegap+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Tim, that is awesome, thank you!! Exactly what I was looking for. Yes, I can confirm that works on android as well.
Dan