Viewed   27 times

I've been trying to get Google Analytics to work in PhoneGap 1.2.0 over Android and iOS.

What are the main differences between Android and iOS when using Google Analytics in PhoneGap 1.2.0?



The three main components required and the different parts for Android and iOS.

  1. GAP-alytics from phonegap-plugins

    • Android
      • analytics.js
    • iOS
      • GoogleAnalyticsPlugin.h / GoogleAnalyticsPlugin.m
      • GoogleAnalyticsPlugin.js
  2. Google Analytics from Google

    • Android
      • libGoogleAnalytics.jar (no source available)
    • iOS - these are included with the phonegap plugin for convenience
      • libGoogleAnalytics.a (no source available)
      • GANTracker.h
  3. PhoneGap from PhoneGap

    • Android
      • add <plugin name="GoogleAnalyticsTracker" value=""/> to res/xml/plugins.xml file
    • iOS
      • In “Supporting Files/PhoneGap.plist” file add:
      • Plugins:
        • key = googleAnalytics (name used in the javascript) Value = GoogleAnalytics (name of Obj-C object)
      • External Hosts
        • ‘*’ (without quotes) as Item 0

NB: Remember that even though the iOS and Android versions have a phonegap-1.2.0.js file they are NOT the same file. The code is different and you can't just copy it between platforms. Make sure your app uses the appropriate version.

Android Javascript

window.plugins.googleAnalytics.start ("your UA code",       //UA-account ID
         function() { console.log("started") },             //successCallBack
         function() { console.log("didn't start") }         //failureCallBack

window.plugins.googleAnalytics.trackPageView (          //**NB**: NOTE CAPITAL 'V'
               "/Main Page",                                    //Page  (include /)
               function() {console.log("tracked page view")},           //successCallBack   
               function() {console.log("didn't track page view")}       //failureCallBack

window.plugins.googleAnalytics.trackEvent (
            "Contact",                                          //Category
            "Email",                                            //Action
            “John Smith”,                                       //Label
            0,                                                  //Value
            function() { console.log("tracked event") },        //successCallBack
            function() { console.log("didn't track event") }    //failureCallBack

iOS Javascript

window.plugins.googleAnalyticsPlugin.startTrackerWithAccountID(“your UA code”); 

window.plugins.googleAnalyticsPlugin.trackPageview(whichPage); //Note lowercase v in trackPageview.    

window.plugins.googleAnalyticsPlugin. googleAnalytics.trackEvent(
    “John Smith”
    function() { console.log("tracked event") },            //successCallBack
    function() { console.log("didn't track event") }        //failureCallBack

NB. the variable ‘whichpage’ MUST be preceded with a forwardslash (/). Android will let you away without this. iOS will NOT.

Tuesday, August 16, 2022

This is not available through the API.

the full list of dimensions and metrics is available at the site:

UPDATE 2016-03-26

Since this answer was posted there were some developments in this area and now this is available in an api. I can't delete this answer since it's accepted check the response from @augustin

The Google Analytics Metadata API The Metadata API returns the list and attributes of columns (i.e. dimensions and metrics) exposed in the Google Analytics reporting APIs. Attributes returned include UI name, description, segments support, and more. At this time the response is only metadata for the Google Analytics Core reporting API V3 and the Google Analytics Reporting API v4. Real-time and MCF metadata is not available.

Saturday, October 29, 2022

First and foremost I have to give a lot of credit to the guys here. Their blog was of tremendous help but I still had to do a few more things to finally get it to work.

I’m going to go through it step by step so that you can get it to work at a very basic level and then you can take it from there. I found that even the slightest mistake can mess things up.

I am assuming you have installed PhoneGap 1.2.0 and are using Xcode 4.2


Get a basic PhoneGap app working. Follow the PhoneGap guide and get that working.


Download the PhoneGap Google Analytics Plugin. I find the easiest thing to do is just download the zip file from here. You get a pile of things you don't need but it doesn't matter.

Expand the file in your download directory. Go into the expanded directory.. into the iPhone folder and you should see the GoogleAnalytics folder.

In Xcode, right click on project (the blue block thing at the top) and choose 'Add files to '. Navigate to the expanded folder. Click on the 'GoogleAnalytics' folder. Make sure the 'copy files into destination group's folder (if necessary)' is selected and that its added to the target. Click 'Add'. Drag the files in the folder in Xcode to the Plugins folder.

Go back to Finder and open the project directory. Copy the GoogleAnalyticsPlugin.js from the GoogleAnalytics folder to the 'www' folder. Xcode will give a warning about re-saving the file.. Just click Close.

Go to the index.html file. In function onDeviceReady() add the following line to the start of the function.

window.plugins.googleAnalyticsPlugin.startTrackerWithAccountID("YOUR UA CODE");

Next add the following function under the onDeviceReady function (inside the same script block)

function trackpage(id)
    console.log("start trackpage: " + id);
    console.log("end trackpage: " + id);

Now, still in the index.html file, find the line

<li>Check your console log for any white-list rejection errors.</li>

(remember this is phonegap 1.2.0)

add the following line after it.

<li class="arrow"><a href="javascript:trackpage('/TEST');">test analytics</a></li>

This gives a link you can click on in the test app.

NB. You MUST have a forward slash (/) in the page your are tracking. So javascript:trackpage('/TEST') will work while javascript:trackpage('TEST') will NOT.

Finally, still in the index.html add the following line

<script type="text/javascript" charset="utf-8" src="GoogleAnalyticsPlugin.js"></script> 


<script type="text/javascript" charset="utf-8" src="phonegap-1.2.0.js"></script>

Don't bother trying to run it yet. It won't work.


Next goto the GoogleAnalytics.h file. Just wipe it.. There are a pile of problems here. JSON seems to be now JSONKit and PhoneGapCommand is now PGPlugin.. Just paste this in. The PhoneGap documentation seems to be all over the place.

#import <Foundation/Foundation.h>

#import <PhoneGap/PGPlugin.h>
#import <PhoneGap/NSData+Base64.h>
#import <PhoneGap/JSONKit.h> 
#import "PGPlugin.h"
#import "NSData+Base64.h"
#import "JSONKit.h" 

#import "GANTracker.h"

@interface GoogleAnalyticsPlugin : PGPlugin<GANTrackerDelegate> {


- (void) startTrackerWithAccountID:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
- (void) trackEvent:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
- (void) trackPageview:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;



Next add the Google Analytics part to the project. This is the part which Google provides but its conveniently included in the phonegap ios GoogleAnalytics/GoogSDK plugin folder.

In Xcode, go to the GoogleAnalytics/GoogSDK folder and drag the GANTracker.h and libGoogleAnalytics.a files to the Plugins directory.

Basically you need to copy them from the ‘blue’ GoogleAnalytics/GoogSDK folder into a ‘yellow’ folder so they can be found by header files.


Next we need to add sqlite (where Google Analytics stores data when not online) and CFNetwork (so Google Analytics can upload the data) to the project.

In Xcode click on the project (the blue thing at the top of the file). Click on the main target. Then click on the Build Phases. The 3rd item on the list is the “Link Binary with Libraries”. Note there are two of these.. choose the top one. Open it and click on the + sign. Add libsqlite3.0.dylib then add CFNetwork.framework.


Finally (almost there) we need to setup the plugin. Go to the PhoneGap.plist file. Under Plugins add another entry with the key as googleAnalyticsPlugin (the javascript name) and the value as GoogleAnalyticsPlugin (the Obj-C part).

Under ExternalHosts add a * entry

Now try and run. Fingers crossed it will work.

The best way to test it to run the program and then use the new Google Analytics Real Time (BETA) mode as you can see pages being tracked live.

You need to change to the new version of Google Analytics (it should be an obvious link at the top to move to the new version). Open the GA account, go to Home..REAL_TIME... Overview. You should see the TEST page track coming in within a few seconds.

Note: You can't track events in realtime. You need to wait for them to be processed.


I had some problems when looking at the Compile Sources and Link Binary With Libraries. The GoogleAnalytics parts would show up in red. If they are in red it means your project can't find them and I think how you resolve this depends on your version of Xcode so you might need to google about a bit to resolve that. What I did was to remove the GoogleAnalytics (press the - sign) then drag the file in. If it shows up as a "proper icon" (as opposed to a red box) it should have worked.

Doing it on Android also?

I assume if you are using Phonegap you are probably doing an Android version also. If this is case you may want to check out this comparison Q+A for the differences.

Thursday, November 24, 2022

After reaching out to the Google Analytics team, they confirmed that betas 3 and 4 of the iOS SDKs have a bug that is responsible for the underreporting that I'm seeing.

Tuesday, December 13, 2022

Turns out the problem was inside the Google Analytics SDK. A new version has been posted:

[Google Analytics SDK issue with short sessions][1]
Wednesday, September 14, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :