Skip to content

Commit

Permalink
Recommit of lost code from destroyed fork.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse committed Oct 5, 2011
1 parent 4415249 commit 58ca387
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 0 deletions.
96 changes: 96 additions & 0 deletions iPhone/GoogleAnalytics/GoogSDK/GANTracker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// GANTracker.h
// Google Analytics iPhone SDK.
// Version: 1.1
//
// Copyright 2009 Google Inc. All rights reserved.
//

extern NSString* const kGANTrackerErrorDomain;
extern NSInteger const kGANTrackerNotStartedError;
extern NSInteger const kGANTrackerInvalidInputError;
extern NSInteger const kGANTrackerEventsPerSessionLimitError;
extern NSUInteger const kGANMaxCustomVariables;
extern NSUInteger const kGANMaxCustomVariableLength;
extern NSUInteger const kGANVisitorScope;
extern NSUInteger const kGANSessionScope;
extern NSUInteger const kGANPageScope;

@protocol GANTrackerDelegate;
typedef struct __GANTrackerPrivate GANTrackerPrivate;

// Google Analytics tracker interface. Tracked pageviews and events are stored
// in a persistent store and dispatched in the background to the server.
@interface GANTracker : NSObject {
@private
GANTrackerPrivate *private_;
}

// Singleton instance of this class for convenience.
+ (GANTracker *)sharedTracker;

// Start the tracker by specifying a Google Analytics account ID and a
// dispatch period (in seconds) to dispatch events to the server
// (or -1 to dispatch manually). An optional delegate may be
// supplied.
- (void)startTrackerWithAccountID:(NSString *)accountID
dispatchPeriod:(NSInteger)dispatchPeriod
delegate:(id<GANTrackerDelegate>)delegate;

// Stop the tracker.
- (void)stopTracker;

// Track a page view. The pageURL must start with a forward
// slash '/'. Returns YES on success or NO on error (with outErrorOrNULL
// set to the specific error).
- (BOOL)trackPageview:(NSString *)pageURL
withError:(NSError **)error;

// Track an event. The category and action are required. The label and
// value are optional (specify nil for no label and -1 or any negative integer
// for no value). Returns YES on success or NO on error (with outErrorOrNULL
// set to the specific error).
- (BOOL)trackEvent:(NSString *)category
action:(NSString *)action
label:(NSString *)label
value:(NSInteger)value
withError:(NSError **)error;

// Set a custom variable. visitor and session scoped custom variables are stored
// for later use. Session and page scoped custom variables are attached to each
// event. Visitor scoped custom variables are sent only on the first event for
// a session.
- (BOOL)setCustomVariableAtIndex:(NSUInteger)index
name:(NSString *)name
value:(NSString *)value
scope:(NSUInteger)scope
withError:(NSError **)error;

// Set a page scoped custom variable. The variable set is returned with the
// next event only. It will overwrite any existing visitor or session scoped
// custom variables.
- (BOOL)setCustomVariableAtIndex:(NSUInteger)index
name:(NSString *)name
value:(NSString *)value
withError:(NSError **)error;

// Returns the value of the custom variable at the index requested. Returns
// nil if no variable is found or index is out of range.
- (NSString *) getVisitorCustomVarAtIndex:(NSUInteger)index;

// Manually dispatch pageviews/events to the server. Returns YES if
// a new dispatch starts.
- (BOOL)dispatch;

@end

@protocol GANTrackerDelegate <NSObject>

// Invoked when a dispatch completes. Reports the number of events
// dispatched and the number of events that failed to dispatch. Failed
// events will be retried on next dispatch.
- (void)trackerDispatchDidComplete:(GANTracker *)tracker
eventsDispatched:(NSUInteger)eventsDispatched
eventsFailedDispatch:(NSUInteger)eventsFailedDispatch;

@end
Binary file not shown.
22 changes: 22 additions & 0 deletions iPhone/GoogleAnalytics/GoogleAnalyticsPlugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// GoogleAnalyticsPlugin.h
// Gapalytics
//
// Created by Jesse MacFadyen on 11-04-21.
// Copyright 2011 Nitobi. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "PhoneGapCommand.h"
#import "GANTracker.h"


@interface GoogleAnalyticsPlugin : PhoneGapCommand<GANTrackerDelegate> {

}

- (void) startTrackerWithAccountID:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
- (void) trackEvent:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
- (void) trackPageview:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;

@end
51 changes: 51 additions & 0 deletions iPhone/GoogleAnalytics/GoogleAnalyticsPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

function GoogleAnalyticsPlugin()
{

}

GoogleAnalyticsPlugin.prototype.startTrackerWithAccountID = function(id)
{
PhoneGap.exec("GoogleAnalyticsPlugin.startTrackerWithAccountID",id);
};

GoogleAnalyticsPlugin.prototype.trackPageview = function(pageUri)
{
PhoneGap.exec("GoogleAnalyticsPlugin.trackPageview",pageUri);
};


GoogleAnalyticsPlugin.prototype.trackEvent = function(category,action,label,value)
{
var options = {category:category,
action:action,
label:label,
value:value};
PhoneGap.exec("GoogleAnalyticsPlugin.trackEvent",options);
};

GoogleAnalyticsPlugin.prototype.trackerDispatchDidComplete = function(count)
{
//console.log("trackerDispatchDidComplete :: " + count);
};

/**
* Install function
*/
GoogleAnalyticsPlugin.install = function()
{
if ( !window.plugins )
{
window.plugins = {};
}
if ( !window.plugins.googleAnalyticsPlugin )
{
window.plugins.googleAnalyticsPlugin = new GoogleAnalyticsPlugin();
}
}

/**
* Add to PhoneGap constructor
*/
PhoneGap.addConstructor(GoogleAnalyticsPlugin.install);

81 changes: 81 additions & 0 deletions iPhone/GoogleAnalytics/GoogleAnalyticsPlugin.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// GoogleAnalyticsPlugin.m
// Gapalytics
//
// Created by Jesse MacFadyen on 11-04-21.
// Copyright 2011 Nitobi. All rights reserved.
//

#import "GoogleAnalyticsPlugin.h"



// Dispatch period in seconds
static const NSInteger kGANDispatchPeriodSec = 10;


@implementation GoogleAnalyticsPlugin

- (void) startTrackerWithAccountID:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
NSString* accountId = [arguments objectAtIndex:0];

[[GANTracker sharedTracker] startTrackerWithAccountID:accountId
dispatchPeriod:kGANDispatchPeriodSec
delegate:self];

}

- (void) trackEvent:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{

NSString* category = [options valueForKey:@"category"];
NSString* action = [options valueForKey:@"action"];
NSString* label = [options valueForKey:@"label"];
int value = [[options valueForKey:@"value"] intValue];

NSError *error;

if (![[GANTracker sharedTracker] trackEvent:category
action:action
label:label
value:value
withError:&error]) {
// Handle error here
NSLog(@"GoogleAnalyticsPlugin.trackEvent Error::",[error localizedDescription]);
}


NSLog(@"GoogleAnalyticsPlugin.trackEvent::%@, %@, %@, %d",category,action,label,value);

}

- (void) trackPageview:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
NSString* pageUri = [arguments objectAtIndex:0];
NSError *error;
if (![[GANTracker sharedTracker] trackPageview:pageUri
withError:&error]) {
// TODO: Handle error here
}
}



- (void)trackerDispatchDidComplete:(GANTracker *)tracker
eventsDispatched:(NSUInteger)eventsDispatched
eventsFailedDispatch:(NSUInteger)eventsFailedDispatch
{
NSString* callback = [NSString stringWithFormat:@"window.plugins.googleAnalyticsPlugin.trackerDispatchDidComplete(%d);",
eventsDispatched];
[ self.webView stringByEvaluatingJavaScriptFromString:callback];

}

- (void) dealloc
{
[[GANTracker sharedTracker] stopTracker];
[ super dealloc ];
}

@end
40 changes: 40 additions & 0 deletions iPhone/GoogleAnalytics/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

iOS Plugin for adding Google Analytics to your PhoneGap application.

Google instructions are here:
http://code.google.com/mobile/analytics/docs/

Supported Platforms:

iOS:
Requirements :
- google lib is included in this repo. in iOS/GoogSDK/
- Web property ID for analytics


Instructions :
- add all files under iOS to the plugins folder of your XCode project ( via XCode Project explorer )
- add GoogleAnalyticsPlugin.js to your www folder ( via Finder )
- include GoogleAnalyticsPlugin.js from index.html

- Google's docs are here : http://code.google.com/mobile/analytics/docs/iphone/

Android :
TODO:



Javascript Interface:

// after device ready, create a local alias and start the tracker with your own id.
var googleAnalytics = window.plugins.googleAnalyticsPlugin;
googleAnalytics.startTrackerWithAccountID("UA-6369089-3");

// Track an event in your application
// more here : http://code.google.com/apis/analytics/docs/tracking/eventTrackerGuide.html
googleAnalytics.trackEvent("category","action","label goes here",666);

// Track an pageview in your application
googleAnalytics.trackPageview(pageURI);


0 comments on commit 58ca387

Please sign in to comment.