How to use Google Analytics with Phonegap without a plugin?

后端 未结 14 2064
北海茫月
北海茫月 2020-11-28 03:03

I am making an app and I want to get analytics from the users. I tried to use the Phonegap Plugin, but I didn\'t have any luck trying to implement it.

I was wonderin

14条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 03:26

    [edit] Google Analytics now works with localstorage in hybrid apps.

    Google Analytics now have an options explained here to use LocalStorage instead of cookies and there is also a hack to make it work in webviews (file:// urls). So instead of using the code I suggested before, you can just do this :

    // THIS IS FOR LOCALSTORAGE
    var GA_LOCAL_STORAGE_KEY = 'ga:clientId';
    ga('create', 'UA-XXXXX-Y', {
      'storage': 'none',
      'clientId': localStorage.getItem(GA_LOCAL_STORAGE_KEY)
    });
    ga(function(tracker) {
      localStorage.setItem(GA_LOCAL_STORAGE_KEY, tracker.get('clientId'));
    });
    
    // THIS IS FOR FILE URL SUPPORT
    ga('set', 'checkProtocolTask', function(){ /* noop */});
    
    // And then as usual...
    ga('send', 'pageview');
    

    previous answer content :

    The pokki solution suggested by Alex is working fine with a few adjustments to remove the need of Pokki.

    I created a git project for this cleaned-up version here :

    https://github.com/ggendre/GALocalStorage

    Works great on android 4.1 and ios6, I will test more device very soon. Hope this helps ! :)

提交回复
热议问题