diff --git a/Android/Analytics/2.0/README.md b/Android/Analytics/2.0/README.md index 21306e32..e1d65eff 100644 --- a/Android/Analytics/2.0/README.md +++ b/Android/Analytics/2.0/README.md @@ -26,11 +26,11 @@ Using this plugin requires [PhoneGap Cordova library for Android](http://phonega </intent-filter>
</activity> -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: - <plugin name="GoogleAnalyticsTracker" value="com.phonegap.plugins.analytics.GoogleAnalyticsTracker" /> + <plugin name="GoogleAnalyticsTracker" value="com.phonegap.plugins.analytics.GoogleAnalyticsTracker" /> ## Using the plugin ## @@ -39,88 +39,62 @@ The plugin creates the object `window.plugins.analytics`. To use, call one of t
 /**
  * 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);
 
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");}); +
 /**
  * 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);
 
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");}); +
 /**
  * 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);
-
- -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");}); - -
-/**
- * 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);
 
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: -
    
-@Override
-public void onDestroy() 
-{
-    super.onDestroy();
-    GoogleAnalyticsTracker tracker = com.google.android.apps.analytics.GoogleAnalyticsTracker.getInstance();
-    tracker.dispatch();
-    tracker.stopSession();
-}
-
## RELEASE NOTES ## +### MAR, 28, 2013 ### + +* Added support for Analytics 2.0 beta + ### AUG, 14, 2012 ### * Added suppport for Cordova 1.9 and above @@ -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. --- @@ -201,11 +175,11 @@ Copyright (c) <2010> 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 \ No newline at end of file diff --git a/Android/Analytics/2.0/lib/libGoogleAnalytics.jar b/Android/Analytics/2.0/lib/libGoogleAnalytics.jar deleted file mode 100644 index 78d47a72..00000000 Binary files a/Android/Analytics/2.0/lib/libGoogleAnalytics.jar and /dev/null differ diff --git a/Android/Analytics/2.0/src/com/phonegap/plugins/analytics/GoogleAnalyticsTracker.java b/Android/Analytics/2.0/src/com/phonegap/plugins/analytics/GoogleAnalyticsTracker.java index c700844d..4c2dffd8 100644 --- a/Android/Analytics/2.0/src/com/phonegap/plugins/analytics/GoogleAnalyticsTracker.java +++ b/Android/Analytics/2.0/src/com/phonegap/plugins/analytics/GoogleAnalyticsTracker.java @@ -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 @@ -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); @@ -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); } } \ No newline at end of file diff --git a/Android/Analytics/2.0/www/analytics.js b/Android/Analytics/2.0/www/analytics.js index 8c31841c..eadf4109 100644 --- a/Android/Analytics/2.0/www/analytics.js +++ b/Android/Analytics/2.0/www/analytics.js @@ -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. */ @@ -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]); }; /** @@ -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) - ]); + ]); }; /**