Recently I integrated Google Analytics into our MySmartPrice Android Application . The application was built using Sencha Touch framework and packaged using PhoneGap. So this post is about integrating Google Analytics with PhoneGap into Sencha Touch Android application.
It is simple to add Google Analytics tracking into your Android Application through phonegap. PhoneGap provides a plugin named GAPlugin which needs to be included into your project. Once included, with less than 10 lines of code, your basic tracking will be available through Google Analytics.
Google Analytics with PhoneGap
I assume you are already familiar with config.xml in PhoneGap. In the file, you need to add the following line of xml,
Once added, save and close your file. Open the index.html where you have included the phonegap.js script. Add the extra lines in to the html file [Do not include phonegap.js twice].
Now we have integrated the plugin into our project. It is time to initialize the component and start pushing information to our analytics account. In the html file, before closing the head, add the following script.
What does the above code do?
On initialization, a listener is added to deviceready event. On device ready, we are initializing the plugin object with our Google Analytics tracking code and time interval between sending events to server. Keep this less to minimize the overhead of sending data from mobile devices. All the functions take first two parameters as functions which will be called on success and failure respectively. In this case, we are just writing an anonymous function which prints the text to console. If you want to take some action based on the feedback, write a function and replace the anonymous function with the function name.
TrackEvent('Login','Click','Some Value','any value');
You should use TrackEvent in case if you wish to track ajax requests and events. The information can be found under Content/Events/Overview in your analytics dashboard. If you wish to know more information on how events work, look at this post.