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?
[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 :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 ! :)
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 prependedhttps:
to GA url underIssue 2. Google by default aborts the request if the page protocol is not http or https. To fix this
And you should be set. After making these changes, I was able to confirm the events on GA. Hope it helps you too.
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
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
Then write the below code under your < head> tag,and its start showing Realtime active Users in your dashboards.
https://github.com/ggendre/GALocalStorage
Quick and dirty solution. Use can use a light-hidden iframe like this;
And every time you request a page in the PhoneGap app, reload this iframe to initialize the track.
You can use
GALocalstorage
library, and it works fine on mobile devicesIt's easy to setup
and that is it, no modification or anything else.
Library on GitHub