How to use Google Analytics with Phonegap without

2019-01-04 19:17发布

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 wondering if it was possible to get Google Analytics by treating the app like a normal webpage and putting some javascript in the head of my page.

Is there a better way to do this? and is the Phonegap Google Analytics THAT much better than what I'm trying to do?

11条回答
Explosion°爆炸
2楼-- · 2019-01-04 19:19

[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 ! :)

查看更多
该账号已被封号
3楼-- · 2019-01-04 19:19

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.

查看更多
Rolldiameter
4楼-- · 2019-01-04 19:20

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

查看更多
做个烂人
5楼-- · 2019-01-04 19:21

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>
查看更多
Melony?
6楼-- · 2019-01-04 19:24

Quick and dirty solution. Use can use a light-hidden iframe like this;

<iframe src="http://www.yourwebsite.com/userControls/GoogleAnalytics.html?param=extraParamHere" style="visibility: hidden"></iframe>

And every time you request a page in the PhoneGap app, reload this iframe to initialize the track.

查看更多
等我变得足够好
7楼-- · 2019-01-04 19:24

You can use GALocalstorage library, and it works fine on mobile devices

It's easy to setup

<script type="text/javascript" src="js/libs/GALocalStorage.js"></script>
<script>
    ga_storage._setAccount('UA-37167XXX-1'); //Replace with your own
    ga_storage._trackPageview('/index.html');

</script>

and that is it, no modification or anything else.

You need to create Website Account in Google analytics to use this library

Library on GitHub

查看更多
登录 后发表回答