Step by step to get Google Analytics working in PhoneGap 1.2.0 on iOS (phonegapalytics)

How to install Google Analytics in PhoneGap 1.2.0 on iOS?

+6
source share
2 answers

First of all, I have to pay tribute to the guys . Their blog was a huge help, but I still had to do a few more things to finally get it working.

I am 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 ruin the situation.

I assume that you have installed PhoneGap 1.2.0 and are using Xcode 4.2


PART A

Get the basic PhoneGap app. Follow the PhoneGap guide and get this working.


PART B

Download the Google Phone Google PhoneGap Plugin. I find that the easiest way is to simply download the zip file from here. You get a bunch of things you don’t need, but it’s not. It does not matter.

Expand the file in the download directory. Go to the extended directory .. in the iPhone folder and you will see the GoogleAnalytics folder.

In Xcode, right-click on the project (blue block thing at the top) and select "Add Files To". Go to the expanded folder. Click on the GoogleAnalytics folder. Make sure that “copy files to destination group folder (if necessary)” is selected and added to the target. Click Add. Drag and drop the files into a folder in Xcode into the Plugins folder.

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

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

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

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

 function trackpage(id) { console.log("start trackpage: " + id); window.plugins.googleAnalyticsPlugin.trackPageview(id); console.log("end trackpage: " + id); } 

Now, while still in index.html, 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 that you can click in the test application.

NB. You MUST have a slash (/) on the page you are tracking. This way javascript:trackpage('/TEST') will work, and javascript:trackpage('TEST') will be NOT.

Finally, back in index.html add the following line

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

below

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

Do not try to run it yet. This will not work.


PART C

Next, go to the GoogleAnalytics.h file. Just wipe it off. There are a lot of problems here. JSON seems to be JSONKit now, and PhoneGapCommand is now PGPlugin .. Just paste this. PhoneGap documentation seems to be everywhere.

 #import <Foundation/Foundation.h> #ifdef PHONEGAP_FRAMEWORK #import <PhoneGap/PGPlugin.h> #import <PhoneGap/NSData+Base64.h> #import <PhoneGap/JSONKit.h> #else #import "PGPlugin.h" #import "NSData+Base64.h" #import "JSONKit.h" #endif #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; @end 

PART D

Then add the Google Analytics part to the project. This is the part that Google provides, but it’s convenient to include it in the GoogleAnalytics / GoogSDK iOS plugins folder.

In Xcode, go to the GoogleAnalytics/GoogSDK and drag and drop the GANTracker.h and libGoogleAnalytics.a into the plugins directory.

Basically you need to copy them from the "blue GoogleAnalytics/GoogSDK folder to the" yellow folder "so that they can be found by the header files.


PART E

Then we need to add sqlite (where Google Analytics stores data not on the Internet) and CFNetwork (so that Google Analytics can load data) into the project.

In Xcode, click on the project (the blue thing at the top of the file). Click on the main target. Then click Build Phases. The third item on the list is "Binary Library Connection." Please note that there are two of them. Choose the top one. Open it and click on the + sign. Add libsqlite3.0.dylib , then add CFNetwork.framework .


PART F

Finally (almost there) we need to configure the plugin. Go to the PhoneGap.plist file. In the "Plugins" section, add another entry with the key as googleAnalyticsPlugin (javascript name) and the value as googleAnalyticsPlugin (part of Obj-C).

In the ExternalHosts section, add the entry *

Now try and run. Fingers crossed will work.

The best way to test it is to run the program, and then use the new Google Analytics (BETA) real-time mode, because you can see how pages are tracked in real time.

You need to upgrade to the new version of Google Analytics (it must be the obvious link at the top to upgrade to the new version). Open your GA account, go to Home..REAL_TIME ... Overview. Within a few seconds, you will see the track of the TEST page.

Note. You cannot track events in real time. You need to wait until they are processed.

QUESTIONS

I am having trouble viewing compilation sources and binary links with libraries. GoogleAnalytics details will be displayed in red. If they are red, it means that your project cannot find them, and I think that you allow it depends on your version of Xcode, so you may need to tell Google a little about it. What I did was delete GoogleAnalytics (click the sign) and then drag and drop the file. If it appears as the “correct icon” (as opposed to red), it should work.

Doing this on Android also?

I assume that if you use Phonegap, you are probably also making an Android version. If so, you can check out this Q + A comparison for differences.

+31
source

Thank you very much for your excellent work, the only vague thing was with this block:

 function trackpage(id) { console.log("start trackpage: " + id); window.plugins.googleAnalyticsPlugin.trackPageview(id); console.log("end trackpage: " + id); } 

If it were outside the onready function, so that it works in my application.

+1
source

Source: https://habr.com/ru/post/901306/


All Articles