Question

We are in the process of turning our native iPad app into a hybrid app. Some functionality and UI will remain in native code and other functionality will be implemented in HTML that will be served from our servers and will also be available offline.

The main issue I encounter now is with using Google Analytics:
The existing native code uses the GA SDK for IOS and I planned on using the web API for the web part, however I can't find how the data from both channels can be used together in GA as the data stores seem to be distinct.

Furthermore, I plan to use Google Analytics' Content Experiments for A/B testing the web part but conversion goals might be ones achieved in the native part.

Anyone have any experience with analytics on hybrid apps or alternative solutions.

Thanks

Was it helpful?

Solution

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.

OTHER TIPS

Indeed a challenging configuration.

Have you looked into using analytics.js (Universal Analytics) for the web part ? Then you may be able to feed data into a single App profile

Else, you could send all the tracking calls from your backend, by using a server side implementation of the Measurement Protocol but you'll probably loose usage of Content Experiment !

I use http://www.flurry.com/ for my apps and Google Analytics for my other stuff. I never mixed both of them in the same app but I'm guessing it's doable. I'd sugest to check out flurry first. There's a good chance it will suffice also for an hybrid app.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top