Search code examples
windowscordovagoogle-analyticswindows-phone-8.1flurry

Analytics in Windows Phone 8.1 mobile app on Cordova


I am struggling to find an analytical tool that would allow me to track usage frequency of my mobile app written in Cordova for Windows Phone 8.1. Is there anything you could recommend?

Flurry doesn't seem to handle such configuration, I couldn't make Google Analytics work. I have tried to use http://googleanalyticssdk.codeplex.com/ yet no data is uploaded to my Google Analytics dashboard.

This is an enterprise application, which isn't distributed via Marketplace.

I would be grateful for your help.


Solution

  • Here you go:

    var TID = 'UA-XXXX';
    var GA_ENDPOINT = 'https://www.google-analytics.com/collect';
    var cid;
    
    // Should be called after deviceReady
    function init() {
        cid = device.uuid; // cordova-plugin-device is required
        var xhr = new XMLHttpRequest();
        var body = 'v=1&t=pageview&tid=' + TID + '&cid=' + cid + '&dp=%2Findex.html';
        xhr.open("POST", GA_ENDPOINT, true)
        xhr.onreadystatechange = function () {
            console.log('asd');
        }
        xhr.send(body);
    }
    
    function sendEvent(category, action, label, value) {
        category = category && encodeURIComponent(category);
        action = action && encodeURIComponent(action);
        label = label && encodeURIComponent(label);
        value = value && encodeURIComponent(value);
    
        var xhr = new XMLHttpRequest();
        var body = 'v=1&t=event&tid=' + TID + '&cid=' + cid +
            '&ec=' + category + '&ea=' + action + '&el=' + label + (value ? '&ev=' + value : '');
        xhr.open("POST", GA_ENDPOINT, true);
        xhr.send(body);
    }
    

    Useful links: