How to use Google Analytics with Phonegap without a plugin?

后端 未结 14 1925
北海茫月
北海茫月 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:17

    Didn't work for me. The problem that google code uses cookies and it doesn't work with file:// urls.

    I found good implementation that uses localStorage in place of cookies: https://developers.pokki.com/docs/tutorials.php

    0 讨论(0)
  • 2020-11-28 03:23

    I would like to add my 2 cents. As everybody here I had no luck with analytics plugins, so started to look for pure javascript solution.

    I followed this answer and it is working for me. Means, I can see in Google Analytics Realtime separated user activity from browser and from iOS simulator. (Have not tested Android yet.)

    I changed script a little.

    1)

    // if we are in the app (the protocol will be file://)
    if(document.URL.indexOf('http://') !== 0){
    

    changed to be

    if(document.URL.indexOf('http') !== 0){
    

    to include https case.

    2) Unrelated to where I am: in browser or mobile app, I track events like this:

    ga('send', 'event', '<myAppName>', '<eventName>');
    

    so I can track different applications under same Analytics Property.

    In Google Analytics I created one Property and under this Property three Views. One of type Web and two of type Mobile.

    Applied custom filter(s) to each view:

    To iOS view applied Include filter on field Operating System with value iOS

    To Android view applied Include filter on field Operating System with value Android

    To Web view applied two Exclude filters on field Operating System with values Android and iOS

    Happy cordoving!

    0 讨论(0)
  • 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 ! :)

    0 讨论(0)
  • 2020-11-28 03:30

    this phonegap plugin will help to integrate the google analytics in phonegap app. Read this blog to learn more how to integrate google analytics in phonegap. you can also download the working demo code from here.

    0 讨论(0)
  • 2020-11-28 03:31

    I was using Ionic app (based on cordova) as a mobile website and GA was working for it. When I shipped the same app to native ios, it stopped working.

    Issue 1.
    On checking the logs of simulator, found that GA was not being loaded correctly. It was trying the load file://. To fix this, I prepended https: to GA url under

    (window,document,'script','//www.google-analytics.com/analytics.js','ga')
    

    Issue 2. Google by default aborts the request if the page protocol is not http or https. To fix this

    ga('set', 'checkProtocolTask', null);
    

    And you should be set. After making these changes, I was able to confirm the events on GA. Hope it helps you too.

    0 讨论(0)
  • 2020-11-28 03:31

    NOTE: Google Analytics Client Traking ID generated for mobile platform only supports for IOS and Android.So if you want to Track your google analytics,Be Sure you have Created it for website. Only Tracking ID for website work with phone gap all platform apps. You can then Simply download GALocalStorage from below link and then place it in you js folder under www folder

    www
     |__
        js
          |__
              GALocalStorage.js
    

    Then write the below code under your < head> tag,and its start showing Realtime active Users in your dashboards.

    https://github.com/ggendre/GALocalStorage

           <script>
            ga_storage._setAccount('UA-XXXXXXXX-X'); //Replace with your own
            ga_storage._trackPageview('Home Screen');
            </script>
    
    0 讨论(0)
提交回复
热议问题