Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analytics v2 working with v2.0 beta 4 #117

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 33 additions & 59 deletions Android/Analytics/2.0/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ Using this plugin requires [PhoneGap Cordova library for Android](http://phonega
&lt;/intent-filter&gt;<br/>
&lt;/activity&gt;

4. Download [GoogleAnalytics](https://developers.google.com/analytics/devguides/collection/android/resources) library (tested with 1.4.2) and copy "lib/libGoogleAnalytics.jar" into the libs directory within your project. You will also need to right click on this file in eclipse and add the jar to the build path.
4. Download [GoogleAnalytics](https://developers.google.com/analytics/devguides/collection/android/resources) library (tested with 2.0 beta 4) and copy "lib/libGoogleAnalytics.jar" into the libs directory within your project. You will also need to right click on this file in eclipse and add the jar to the build path.

5. In your res/xml/config.xml file add the following line:

&lt;plugin name=&quot;GoogleAnalyticsTracker&quot; value=&quot;com.phonegap.plugins.analytics.GoogleAnalyticsTracker&quot; /&gt;
&lt;plugin name=&quot;GoogleAnalyticsTracker&quot; value=&quot;com.phonegap.plugins.analytics.GoogleAnalyticsTracker&quot; /&gt;

## Using the plugin ##

Expand All @@ -39,88 +39,62 @@ The plugin creates the object `window.plugins.analytics`. To use, call one of t
<pre>
/**
* Initialize Google Analytics configuration
*
* @param accountId The Google Analytics account id
* @param successCallback The success callback
* @param failureCallback The error callback
*
* @param accountId The Google Analytics account id
* @param successCallback The success callback
* @param failureCallback The error callback
*/

start(accountId, successCallback, failureCallback);
</pre>

Sample use:

window.plugins.analytics.start("Your-Account-ID-Here", function(){alert("Start: success");}, function(){alert("Start: failure");});
window.plugins.analytics.start("Your-Account-ID-Here", function(){alert("Start: success");}, function(){alert("Start: failure");});

<pre>
/**
* Track a page view on Google Analytics
* @param key The name of the tracked item (can be a url or some logical name).
* The key name will be presented in Google Analytics report.
* @param successCallback The success callback
* @param failureCallback The error callback
* @param key The name of the tracked item (can be a url or some logical name).
* The key name will be presented in Google Analytics report.
* @param successCallback The success callback
* @param failureCallback The error callback
*/
trackPageView(key, successCallback, failureCallback);

sendView(key, successCallback, failureCallback);
</pre>

Sample use:

window.plugins.analytics.trackPageView("page1.html", function(){alert("Track: success");}, function(){alert("Track: failure");});
window.plugins.analytics.sendView("page1.html", function(){alert("Track: success");}, function(){alert("Track: failure");});

<pre>
/**
* Track an event on Google Analytics
* @param category The name that you supply as a way to group objects that you want to track
* @param action The name the type of event or interaction you want to track for a particular web object
* @param label Provides additional information for events that you want to track (optional)
* @param value Assign a numerical value to a tracked page object (optional)

* @param successCallback The success callback
* @param failureCallback The error callback
*/

trackEvent(category, action, label, value, successCallback, failureCallback);
</pre>

Sample use:
* @param category The name that you supply as a way to group objects that you want to track
* @param action The name the type of event or interaction you want to track for a particular web object
* @param label Provides additional information for events that you want to track (optional)
* @param value Assign a numerical value to a tracked page object (optional)

window.plugins.analytics.trackEvent("category", "action", "event", 1, function(){alert("Track: success");}, function(){alert("Track: failure");});

<pre>
/**
* Set a custom variable on Google Analytics
* @param index The slot for the custom variable
* @param label The name for the custom variable
* @param value The value for the custom variable
* @param scope The scope for the custom variable (optional)

* @param successCallback The success callback
* @param failureCallback The error callback
* @param successCallback The success callback
* @param failureCallback The error callback
*/

setCustomVar(index, label, value, scope, successCallback, failureCallback);
sendEvent(category, action, label, value, successCallback, failureCallback);
</pre>

Sample use:

window.plugins.analytics.setCustomVar(1, "type", "android", null, function(){alert("SetVar: success");}, function(){alert("SetVar: failure");});
window.plugins.analytics.sendEvent("category", "action", "event", 1, function(){alert("Track: success");}, function(){alert("Track: failure");});

Please keep in mind that these methods, as in any other plugin, are ready to be invoked only after '[deviceready](http://docs.phonegap.com/phonegap_events_events.md.html#deviceready)' event has been fired
Good practice will be manual dispatch and stop session. Add this code to your main activity:
<pre>
@Override
public void onDestroy()
{
super.onDestroy();
GoogleAnalyticsTracker tracker = com.google.android.apps.analytics.GoogleAnalyticsTracker.getInstance();
tracker.dispatch();
tracker.stopSession();
}
</pre>

## RELEASE NOTES ##

### MAR, 28, 2013 ###

* Added support for Analytics 2.0 beta

### AUG, 14, 2012 ###

* Added suppport for Cordova 1.9 and above
Expand All @@ -146,7 +120,7 @@ the directories in which they reside and in the code itself. No external
contributions are allowed under licenses which are fundamentally incompatible
with the MIT or BSD licenses that PhoneGap is distributed under.

The text of the MIT and BSD licenses is reproduced below.
The text of the MIT and BSD licenses is reproduced below.

---

Expand Down Expand Up @@ -201,11 +175,11 @@ Copyright (c) <2010> <Nitobi Software Inc., et. al., >
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

---

### libGoogleAnalytics.jar

The libGoogleAnalytics.jar is distributed under Apache License, Version 2.0.
License URL: http://www.apache.org/licenses/LICENSE-2.0
libGoogleAnalytics.jar URL: http://code.google.com/p/android-scripting/source/browse/android/AndroidScriptingEnvironment/libs/libGoogleAnalytics.jar?r=41b40b84919bdf461784fd86e6ae464697d2abea
Binary file removed Android/Analytics/2.0/lib/libGoogleAnalytics.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@
import org.json.JSONArray;
import org.json.JSONException;

import android.util.Log;
import com.google.analytics.tracking.android.GoogleAnalytics;
import com.google.analytics.tracking.android.Tracker;

public class GoogleAnalyticsTracker extends Plugin {
public static final String START = "start";
public static final String TRACK_PAGE_VIEW = "trackPageView";
public static final String TRACK_EVENT = "trackEvent";
public static final String SET_CUSTOM_VARIABLE = "setCustomVariable";

public static final String SEND_VIEW = "sendView";
public static final String SEND_EVENT = "sendEvent";

public static final int DISPATCH_INTERVAL = 20;
private com.google.android.apps.analytics.GoogleAnalyticsTracker tracker;
private Tracker mGaTracker;
private GoogleAnalytics mGaInstance;

public GoogleAnalyticsTracker() {
tracker = com.google.android.apps.analytics.GoogleAnalyticsTracker.getInstance();
}

@Override
Expand All @@ -38,23 +39,16 @@ public PluginResult execute(String action, JSONArray data, String callbackId) {
} catch (JSONException e) {
result = new PluginResult(Status.JSON_EXCEPTION);
}
} else if (TRACK_PAGE_VIEW.equals(action)) {
try {
trackPageView(data.getString(0));
result = new PluginResult(Status.OK);
} catch (JSONException e) {
result = new PluginResult(Status.JSON_EXCEPTION);
}
} else if (TRACK_EVENT.equals(action)) {
} else if (SEND_VIEW.equals(action)) {
try {
trackEvent(data.getString(0), data.getString(1), data.getString(2), data.getInt(3));
sendView(data.getString(0));
result = new PluginResult(Status.OK);
} catch (JSONException e) {
result = new PluginResult(Status.JSON_EXCEPTION);
}
} else if (SET_CUSTOM_VARIABLE.equals(action)){
} else if (SEND_EVENT.equals(action)) {
try {
setCustomVar(data.getInt(0), data.getString(1), data.getString(2), data.getInt(3));
sendEvent(data.getString(0), data.getString(1), data.getString(2), data.getLong(3));
result = new PluginResult(Status.OK);
} catch (JSONException e) {
result = new PluginResult(Status.JSON_EXCEPTION);
Expand All @@ -65,20 +59,16 @@ public PluginResult execute(String action, JSONArray data, String callbackId) {
return result;
}

private void start(String accountId) {
tracker.startNewSession(accountId, DISPATCH_INTERVAL, this.cordova.getActivity());
private void start(String accountId) {
mGaInstance = GoogleAnalytics.getInstance(this.cordova.getActivity());
mGaTracker = mGaInstance.getTracker(accountId);
}

private void trackPageView(String key) {
tracker.trackPageView(key);
}

private void trackEvent(String category, String action, String label, int value){
tracker.trackEvent(category, action, label, value);
private void sendView(String key) {
mGaTracker.sendView(key);
}

private void setCustomVar(int index, String label, String value, int scope) {
if(scope > 0) tracker.setCustomVar(index, label, value, scope);
else tracker.setCustomVar(index, label, value);
private void sendEvent(String category, String action, String label, Long value){
mGaTracker.sendEvent(category, action, label, value);
}
}
73 changes: 24 additions & 49 deletions Android/Analytics/2.0/www/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* PhoneGap is available under *either* the terms of the modified BSD license *or* the
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
*
* Copyright (c) 2006-2011 Worklight, Ltd.
* Copyright (c) 2006-2011 Worklight, Ltd.
*/


Expand All @@ -13,34 +13,34 @@ var Analytics = function () {};

/**
* Initialize Google Analytics configuration
*
* @param accountId The Google Analytics account id
*
* @param accountId The Google Analytics account id
* @param successCallback The success callback
* @param failureCallback The error callback
*/
Analytics.prototype.start = function(accountId, successCallback, failureCallback) {
return cordova.exec(
successCallback,
failureCallback,
'GoogleAnalyticsTracker',
'start',
successCallback,
failureCallback,
'GoogleAnalyticsTracker',
'start',
[accountId]);
};

/**
* Track a page view on Google Analytics
* Send a page view on Google Analytics
* @param key The name of the tracked item (can be a url or some logical name).
* The key name will be presented in Google Analytics report.
* The key name will be presented in Google Analytics report.
* @param successCallback The success callback
* @param failureCallback The error callback
* @param failureCallback The error callback
*/
Analytics.prototype.trackPageView = function(key, successCallback, failureCallback) {
Analytics.prototype.sendView = function(key, successCallback, failureCallback) {
return cordova.exec(
successCallback,
failureCallback,
successCallback,
failureCallback,
'GoogleAnalyticsTracker',
'trackPageView',
[key]);
'sendView',
[key]);
};

/**
Expand All @@ -51,46 +51,21 @@ Analytics.prototype.trackPageView = function(key, successCallback, failureCallba
* @param value Assign a numerical value to a tracked page object (optional)

* @param successCallback The success callback
* @param failureCallback The error callback
* @param failureCallback The error callback
*/

Analytics.prototype.trackEvent = function(category, action, label, value, successCallback, failureCallback){
Analytics.prototype.sendEvent = function(category, action, label, value, successCallback, failureCallback){
return cordova.exec(
successCallback,
failureCallback,
successCallback,
failureCallback,
'GoogleAnalyticsTracker',
'trackEvent',
'sendEvent',
[
category,
action,
typeof label === "undefined" ? "" : label,
category,
action,
typeof label === "undefined" ? "" : label,
(isNaN(parseInt(value,10))) ? 0 : parseInt(value, 10)
]);
};

/**
* Set a custom variable on Google Analytics
* @param index The slot for the custom variable
* @param label The name for the custom variable
* @param value The value for the custom variable
* @param scope The scope for the custom variable (optional)

* @param successCallback The success callback
* @param failureCallback The error callback
*/

Analytics.prototype.setCustomVar = function(index, label, value, scope, successCallback, failureCallback){
return cordova.exec(
successCallback,
failureCallback,
'GoogleAnalyticsTracker',
'setCustomVariable',
[
(isNaN(parseInt(index,10))) ? 0 : parseInt(index, 10),
label,
value,
(isNaN(parseInt(scope,10))) ? 0 : parseInt(scope, 10)
]);
]);
};

/**
Expand Down