Skip to content

Commit

Permalink
This fixes #8 and fixes #10
Browse files Browse the repository at this point in the history
- setting the log level
- defaults for label and value
  • Loading branch information
Craig MacKay committed Sep 21, 2014
1 parent d1c2694 commit 41cf8b4
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 13 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ analytics.sendAppView('home', successCallback, errorCallback);

var Fields = analytics.Fields,
HitTypes = analytics.HitTypes,
LogLevel = analytics.LogLevel,
params = {};

params[Fields.HIT_TYPE] = HitTypes.APP_VIEW;
params[Fields.SCREEN_NAME] = 'home';

analytics.setLogLevel()

analytics.send(params, successCallback, errorCallback);

```
Expand All @@ -50,6 +53,9 @@ analytics.Fields
// object containing hit type mappings
analytics.HitTypes

// object containing log level constants
analytics.LogLevel (VERBOSE, INFO, WARNING, ERROR)

// all of the function support success and error callback functions

// Sets the tracking id (must be called first)
Expand All @@ -59,6 +65,13 @@ analytics.HitTypes
// error - Function (optional)
analytics.setTrackingId(trackingId, success, error);

// Sets the log level
//
// logLevel - Number (required)
// success - Function (optional)
// error - Function (optional)
analytics.setLogLevel(logLevel, success, error);

// Sends an app view hit
//
// screenName - String (required)
Expand All @@ -70,8 +83,8 @@ analytic.sendAppView(screenName, success, error);
//
// category - String (required)
// action - String (required)
// label - String (optional)
// value - Number (optional)
// label - String (optional, defaults to '')
// value - Number (optional, defaults to 0)
// success - Function (optional)
// error - Function (optional)
analytic.sendEvent(category, action, label, value, success, error);
Expand Down Expand Up @@ -129,6 +142,3 @@ analytics.send(params, success, error);
analytics.close(success, error);

```



28 changes: 26 additions & 2 deletions android/GoogleAnalyticsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ public class GoogleAnalyticsPlugin extends CordovaPlugin {
private static Thread.UncaughtExceptionHandler uncaughtExceptionHandler;

private static final int GA_DISPATCH_PERIOD = 10;
private static final LogLevel GA_LOG_LEVEL = LogLevel.VERBOSE;

private void initializeGa() {
ga = GoogleAnalytics.getInstance(cordova.getActivity());
ga.getLogger().setLogLevel(GA_LOG_LEVEL);

serviceManager = GAServiceManager.getInstance();
serviceManager.setLocalDispatchPeriod(GA_DISPATCH_PERIOD);
Expand Down Expand Up @@ -87,6 +85,11 @@ public boolean execute(String action, JSONArray args, CallbackContext callback)
callback.success();
return true;

} else if ("setLogLevel".equals(action)) {
setLogLevel(args.getInt(0));
callback.success();
return true;

} else if ("get".equals(action)) {
callback.success(get(args.getString(0)));
return true;
Expand Down Expand Up @@ -124,6 +127,27 @@ private void setTrackingId(String trackingId) {
tracker, serviceManager, uncaughtExceptionHandler, cordova.getActivity()));
}

private void setLogLevel(int level) {
LogLevel logLevel = null;
switch (level) {
case 0:
logLevel = LogLevel.VERBOSE;
break;
case 1:
logLevel = LogLevel.INFO;
break;
case 2:
logLevel = LogLevel.WARNING;
break;
case 3:
logLevel = LogLevel.ERROR;
break;
}
if (logLevel != null) {
ga.getLogger().setLogLevel(logLevel);
}
}

private String get(String key) {
assertTracker();
return tracker.get(key);
Expand Down
1 change: 1 addition & 0 deletions ios/GoogleAnalyticsPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
}

- (void) setTrackingId: (CDVInvokedUrlCommand*)command;
- (void) setLogLevel: (CDVInvokedUrlCommand*)command;
- (void) get: (CDVInvokedUrlCommand*)command;
- (void) set: (CDVInvokedUrlCommand*)command;
- (void) send: (CDVInvokedUrlCommand*)command;
Expand Down
14 changes: 13 additions & 1 deletion ios/GoogleAnalyticsPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ - (void) setTrackingId: (CDVInvokedUrlCommand*)command

[GAI sharedInstance].dispatchInterval = 10;
[GAI sharedInstance].trackUncaughtExceptions = YES;
[[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];

if (tracker) {
[[GAI sharedInstance] removeTrackerByName:[tracker name]];
Expand All @@ -43,6 +42,19 @@ - (void) setTrackingId: (CDVInvokedUrlCommand*)command
[self.commandDelegate sendPluginResult:result callbackId:[command callbackId]];
}

- (void) setLogLevel: (CDVInvokedUrlCommand*)command
{
CDVPluginResult* result = nil;

GAILogLevel logLevel = (GAILogLevel)[command.arguments objectAtIndex:0];

[[[GAI sharedInstance] logger] setLogLevel:logLevel];

result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];

[self.commandDelegate sendPluginResult:result callbackId:[command callbackId]];
}

- (void) get: (CDVInvokedUrlCommand*)command
{
CDVPluginResult* result = nil;
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="com.cmackay.plugins.googleanalytics"
version="0.1.1">
version="0.1.2">

<engines>
<engine name="cordova" version=">=3.0.0" />
Expand Down
33 changes: 29 additions & 4 deletions www/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
'use strict';

var argscheck = require('cordova/argscheck'),
exec = require('cordova/exec');
exec = require('cordova/exec'),
platform = require('cordova/platform');

var Fields = {
ANDROID_APP_UID: 'AppUID',
Expand Down Expand Up @@ -95,6 +96,20 @@ var HitTypes = {
TRANSACTION: 'transaction'
};

var LogLevel = {
VERBOSE: 0,
INFO: 1,
WARNING: 2,
ERROR: 3
};

var logLevelCount = 0, key;
for (key in LogLevel) {
if (LogLevel.hasOwnProperty(key)) {
logLevelCount++;
}
}

function Analytics() {
}

Expand All @@ -104,11 +119,22 @@ Analytics.prototype = {

HitTypes: HitTypes,

LogLevel: LogLevel,

setTrackingId: function (trackingId, success, error) {
argscheck.checkArgs('sFF', 'GoogleAnalytics.setTrackingId', arguments);
exec(success, error, 'GoogleAnalytics', 'setTrackingId', [trackingId]);
},

setLogLevel: function (logLevel, success, error) {
argscheck.checkArgs('nFF', 'GoogleAnalytics.setLogLevel', arguments);
if (platform.id === 'ios') {
// the log levels for android are 0,1,2,3 and for ios are 4,3,2,1
logLevel = logLevelCount - logLevel;
}
exec(success, error, 'GoogleAnalytics', 'setLogLevel', [logLevel]);
},

get: function (key, success, error) {
argscheck.checkArgs('sfF', 'GoogleAnalytics.get', arguments);
exec(success, error, 'GoogleAnalytics', 'get', [key]);
Expand Down Expand Up @@ -145,8 +171,8 @@ Analytics.prototype = {
params[Fields.HIT_TYPE] = HitTypes.EVENT;
params[Fields.EVENT_CATEGORY] = category;
params[Fields.EVENT_ACTION] = action;
params[Fields.EVENT_LABEL] = label;
params[Fields.EVENT_VALUE] = value;
params[Fields.EVENT_LABEL] = label || '';
params[Fields.EVENT_VALUE] = value || 0;
this.send(params, success, error);
},

Expand All @@ -170,4 +196,3 @@ Analytics.prototype = {
};

module.exports = new Analytics();

0 comments on commit 41cf8b4

Please sign in to comment.