You really want to use the SDK. It has some features that will come handy for mobile apps like crashes, play store integration. It also sends data in batches to improve battery usage and also can queue hits while the app is offline to be sent when online. You won't be able to emulate that with the Javascript implementations.
So what you need to write is Javascript methods that send data from the WebView back to the Native Part of the App. This other Stack Overflow thread has more detail on how to do that.
So the javascript to track Google Analytics interactions could look something like this.
var _gaq = {};
_gaq.push = function(arr){
var i, hit;
hit = arr.slice(1).join('&');
location.href = 'analytics://'+arr[0]+'?'+arr;
};
Now this will work as a replacement for your ga.js file, you can still use the same API as you use on _gaq today on your Web App, and the adapter above will sends its data to te native part of the APP. And then you just need to write the native part that will intercept that HTTP request and use the native SDK to issue the Google Analytics functions.
A normal _gaq.push(['_trackPageview', '/homepage']);
will become a uri like analytics://_trackPageview?/homepage
, now you just need to intercept and parse that on the Native part.
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [request URL];
NSLog(@"Hit detected %@", url.absoluteString);
if ([[url scheme] isEqualToString:@"analytics"]) {
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
if ([url.host isEqualToString:@"_trackPageview"]) {
// Get the page from parameters and then track the native View.
// (...)
[tracker trackView:page];
}
else if ([url.host isEqualToString:@"_trackEvent"]) {
// Get the event parameters from url parameters and then track the native GA Event.
// (...)
[tracker trackEventWithCategory:cat
withAction:act
withLabel:lab
withValue:val];
}
// Check for all other analytics functions types
// (...)
// Cancel the request
return NO;
}
// Not an analytics: request.
return YES;
}
I hope it have given you a good starting point. Good luck.