Using google analytics with hybrid mobile app
Asked Answered
T

3

15

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

Themistocles answered 10/3, 2013 at 16:25 Comment(0)
W
18

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.

Wheelsman answered 17/3, 2013 at 5:11 Comment(0)
S
1

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 !

Skyline answered 13/3, 2013 at 15:49 Comment(1)
I wasn't expecting this to be a challenging configuration as hybrid apps are the direction many head to, but it proves challenging indeed.Themistocles
B
0

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.

Burghley answered 10/3, 2013 at 20:1 Comment(1)
Have you ever used flurry in the hybrid app ? I don't see any library support yet, perhaps I need to look harder\Liquidambar

© 2022 - 2024 — McMap. All rights reserved.