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

some improvements #4

Open
wants to merge 7 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
5 changes: 4 additions & 1 deletion Google Analytics SDK for OSX/Classes/AnalyticsHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
// http://github.com/noahsw/google-analytics-sdk-for-osx
//


#define GANALYTICS_DOMAIN @"app.ga"

@interface AnalyticsHelper : NSObject

+(BOOL) fireEvent: (NSString*)eventAction eventValue:(NSNumber*)eventValue;

+(BOOL) measureEvent: (NSString*)eventAction eventValue:(NSNumber*)eventValue;


@end
48 changes: 47 additions & 1 deletion Google Analytics SDK for OSX/Classes/AnalyticsHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#import "TrackingRequest.h"
#import "RequestFactory.h"

#import "GoogleUniversalTracking.h"

#import "DDLog.h"
static const int ddLogLevel = LOG_LEVEL_VERBOSE | LOG_LEVEL_INFO | LOG_LEVEL_ERROR | LOG_LEVEL_WARN;

Expand Down Expand Up @@ -45,7 +47,7 @@ +(BOOL) fireEvent: (NSString*)eventAction eventValue:(NSNumber*)eventValue

DDLogInfo(@"%@, %@, %@, %@", eventCategory, eventAction, eventLabel, eventValue);

GoogleEvent* googleEvent = [[GoogleEvent alloc] initWithParams:@"highlighthunter.com" category:eventCategory action:eventAction label:eventLabel value:eventValue];
GoogleEvent* googleEvent = [[GoogleEvent alloc] initWithParams:GANALYTICS_DOMAIN category:eventCategory action:eventAction label:eventLabel value:eventValue];


if (googleEvent != nil)
Expand All @@ -70,7 +72,51 @@ +(BOOL) fireEvent: (NSString*)eventAction eventValue:(NSNumber*)eventValue

}

+(BOOL) measureEvent: (NSString*)eventAction eventValue:(NSNumber*)eventValue {
NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
NSString* eventCategory = [NSString stringWithFormat:@"Mac %@", infoDict[@"CFBundleShortVersionString"]];

NSString* eventLabel = @"empty";
NSUserDefaults* standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults)
{
NSString* userUUID = [standardUserDefaults stringForKey:@"UserUUID"];
if ([userUUID length] == 0)
{ // generate one for the first time
userUUID = [self UUIDString];
[standardUserDefaults setObject:userUUID forKey:@"UserUUID"];
[standardUserDefaults synchronize];
}

eventLabel = userUUID;
}

DDLogInfo(@"%@, %@, %@, %@", eventCategory, eventAction, eventLabel, eventValue);

GoogleEvent* googleEvent = [[GoogleEvent alloc] initWithParams:GANALYTICS_DOMAIN category:eventCategory action:eventAction label:eventLabel value:eventValue];


if (googleEvent != nil)
{
RequestFactory* requestFactory = [RequestFactory new];
TrackingRequest* request = [requestFactory buildRequest:googleEvent];

if (operationQueue == nil)
{
operationQueue = [NSOperationQueue new];
operationQueue.maxConcurrentOperationCount = 1;
}

GoogleUniversalTracking* trackingOperation = [GoogleUniversalTracking new];
trackingOperation.request = request;

[operationQueue addOperation:trackingOperation];

}

return YES;

}

+(NSString*)UUIDString
{
Expand Down
7 changes: 5 additions & 2 deletions Google Analytics SDK for OSX/Classes/GoogleTracking.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ - (id)init
return self;
}



- (void)main
{
if ([NSThread isMainThread]) {
[self performSelectorInBackground:@selector(main) withObject:nil];
return;
}

[NSThread setThreadPriority:0.1];
[NSThread sleepForTimeInterval:1];

Expand Down
16 changes: 16 additions & 0 deletions Google Analytics SDK for OSX/Classes/GoogleUniversalTracking.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// GoogleMeasurementProtocolEvent.h
// Google Analytics SDK for OSX
//
// Created by Jerry Tian on 3/25/13.
// Copyright (c) 2013 Noah Spitzer-Williams. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "TrackingRequest.h"

@interface GoogleUniversalTracking : NSOperation

@property (strong) TrackingRequest* request;

@end
79 changes: 79 additions & 0 deletions Google Analytics SDK for OSX/Classes/GoogleUniversalTracking.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//
// GoogleMeasurementProtocolEvent.m
// Google Analytics SDK for OSX
//
// Created by Jerry Tian on 3/25/13.
// Copyright (c) 2013 Noah Spitzer-Williams. All rights reserved.
//

#import "GoogleUniversalTracking.h"
#import "PublicIP.h"
#import "JFUrlUtil.h"

#import "DDLog.h"
static const int ddLogLevel = LOG_LEVEL_VERBOSE | LOG_LEVEL_INFO | LOG_LEVEL_ERROR | LOG_LEVEL_WARN;

@implementation GoogleUniversalTracking


- (void) main {
if ([NSThread isMainThread]) {
[self performSelectorInBackground:@selector(main) withObject:nil];
return;
}

[NSThread setThreadPriority:0.1];
[NSThread sleepForTimeInterval:1];

static NSString* analyticsAccountCode = nil;
if (analyticsAccountCode == nil) {
NSBundle* mainBundle = [NSBundle mainBundle];
#if DEBUG
analyticsAccountCode = [mainBundle objectForInfoDictionaryKey:@"Google Analytics ID (Debug)"];
#else
analyticsAccountCode = [mainBundle objectForInfoDictionaryKey:@"Google Analytics ID (Release)"];
#endif
}

NSAssert(analyticsAccountCode!=nil, @"analyticsAccountCode can not be nil, set it in your info.plist.");

//the origin author using the fields this way:
//label => client UUID
//category => client version
//action => event name
//value => event value(number)
GoogleEvent * event = self.request.trackingEvent;


GoogleEvent* trackingEvent = [self.request trackingEvent];
//see "Event tracking para":
//https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide
NSString * payload = [NSString stringWithFormat:@"v=1&tid=%@&cid=%@&t=event&ec=%@&ea=%@&el=%@&ev=%@",
analyticsAccountCode,
[JFUrlUtil encodeUrl:event.label],
[JFUrlUtil encodeUrl:event.category],
[JFUrlUtil encodeUrl:event.action],
@"",
event.val
];




@try {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString: @"http://www.google-analytics.com/collect"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody:[payload dataUsingEncoding: NSUTF8StringEncoding]];
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];

if (returnData && [returnData length] > 0)
DDLogInfo(@"Analytics event (%@, %@, %@, %@): triggered successfully!", [trackingEvent category], [trackingEvent action], [trackingEvent label], [trackingEvent val]);
else
DDLogInfo(@"Analytics event (%@, %@, %@, %@): trigger error", [trackingEvent category], [trackingEvent action], [trackingEvent label], [trackingEvent val]);
}
@catch (NSException *exception) {
DDLogInfo(@"Analytics event (%@, %@, %@, %@): exception thrown %@", [trackingEvent category], [trackingEvent action], [trackingEvent label], [trackingEvent val], exception);
}
}
@end
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
objects = {

/* Begin PBXBuildFile section */
C469A05F170011780085BB41 /* GoogleUniversalTracking.h in Headers */ = {isa = PBXBuildFile; fileRef = C469A05D170011780085BB41 /* GoogleUniversalTracking.h */; };
C469A060170011780085BB41 /* GoogleUniversalTracking.m in Sources */ = {isa = PBXBuildFile; fileRef = C469A05E170011780085BB41 /* GoogleUniversalTracking.m */; };
CE42F10B166AE7FE005AE3D4 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CE42F10A166AE7FE005AE3D4 /* Cocoa.framework */; };
CE42F115166AE7FE005AE3D4 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = CE42F113166AE7FE005AE3D4 /* InfoPlist.strings */; };
CE42F121166AE7FE005AE3D4 /* SenTestingKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CE42F120166AE7FE005AE3D4 /* SenTestingKit.framework */; };
CE42F122166AE7FE005AE3D4 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CE42F10A166AE7FE005AE3D4 /* Cocoa.framework */; };
CE42F125166AE7FE005AE3D4 /* Google Analytics SDK for OSX.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CE42F107166AE7FE005AE3D4 /* Google Analytics SDK for OSX.framework */; };
CE42F12B166AE7FE005AE3D4 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = CE42F129166AE7FE005AE3D4 /* InfoPlist.strings */; };
CE42F12E166AE7FF005AE3D4 /* Google_Analytics_SDK_for_OSXTests.m in Sources */ = {isa = PBXBuildFile; fileRef = CE42F12D166AE7FF005AE3D4 /* Google_Analytics_SDK_for_OSXTests.m */; };
CE42F145166AE977005AE3D4 /* AnalyticsHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = CE42F137166AE976005AE3D4 /* AnalyticsHelper.h */; };
CE42F145166AE977005AE3D4 /* AnalyticsHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = CE42F137166AE976005AE3D4 /* AnalyticsHelper.h */; settings = {ATTRIBUTES = (Public, ); }; };
CE42F146166AE977005AE3D4 /* AnalyticsHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = CE42F138166AE976005AE3D4 /* AnalyticsHelper.m */; };
CE42F147166AE977005AE3D4 /* GoogleEvent.h in Headers */ = {isa = PBXBuildFile; fileRef = CE42F139166AE976005AE3D4 /* GoogleEvent.h */; };
CE42F148166AE977005AE3D4 /* GoogleEvent.m in Sources */ = {isa = PBXBuildFile; fileRef = CE42F13A166AE976005AE3D4 /* GoogleEvent.m */; };
Expand Down Expand Up @@ -47,6 +49,8 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
C469A05D170011780085BB41 /* GoogleUniversalTracking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GoogleUniversalTracking.h; sourceTree = "<group>"; };
C469A05E170011780085BB41 /* GoogleUniversalTracking.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GoogleUniversalTracking.m; sourceTree = "<group>"; };
CE42F107166AE7FE005AE3D4 /* Google Analytics SDK for OSX.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = "Google Analytics SDK for OSX.framework"; sourceTree = BUILT_PRODUCTS_DIR; };
CE42F10A166AE7FE005AE3D4 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
CE42F10D166AE7FE005AE3D4 /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -152,6 +156,8 @@
CE42F13A166AE976005AE3D4 /* GoogleEvent.m */,
CE42F13B166AE976005AE3D4 /* GoogleTracking.h */,
CE42F13C166AE977005AE3D4 /* GoogleTracking.m */,
C469A05D170011780085BB41 /* GoogleUniversalTracking.h */,
C469A05E170011780085BB41 /* GoogleUniversalTracking.m */,
CE816B8F166AF35D00D1FF53 /* JFUrlUtil.h */,
CE816B90166AF35D00D1FF53 /* JFUrlUtil.m */,
CE42F13D166AE977005AE3D4 /* NSString+URLEncoding.h */,
Expand Down Expand Up @@ -221,6 +227,7 @@
CE42F151166AE977005AE3D4 /* TrackingRequest.h in Headers */,
CE42F156166AEFA6005AE3D4 /* DDLog.h in Headers */,
CE816B91166AF35D00D1FF53 /* JFUrlUtil.h in Headers */,
C469A05F170011780085BB41 /* GoogleUniversalTracking.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -340,6 +347,7 @@
CE42F152166AE977005AE3D4 /* TrackingRequest.m in Sources */,
CE42F157166AEFA6005AE3D4 /* DDLog.m in Sources */,
CE816B92166AF35D00D1FF53 /* JFUrlUtil.m in Sources */,
C469A060170011780085BB41 /* GoogleUniversalTracking.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -439,29 +447,33 @@
CE42F132166AE7FF005AE3D4 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
COMBINE_HIDPI_IMAGES = YES;
COMBINE_HIDPI_IMAGES = NO;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
FRAMEWORK_VERSION = A;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Classes/Google Analytics SDK for OSX-Prefix.pch";
INFOPLIST_FILE = "Classes/Google Analytics SDK for OSX-Info.plist";
INSTALL_PATH = "@executable_path/../Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = NO;
WRAPPER_EXTENSION = framework;
};
name = Debug;
};
CE42F133166AE7FF005AE3D4 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
COMBINE_HIDPI_IMAGES = YES;
COMBINE_HIDPI_IMAGES = NO;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
FRAMEWORK_VERSION = A;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Classes/Google Analytics SDK for OSX-Prefix.pch";
INFOPLIST_FILE = "Classes/Google Analytics SDK for OSX-Info.plist";
INSTALL_PATH = "@executable_path/../Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = NO;
WRAPPER_EXTENSION = framework;
};
name = Release;
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This unofficial SDK allows you to track usage of your OS X applications. Google

Instructions
------------
1. Add your Google Analytics ID to the project pList
1. Add your Google Analytics ID to the project pList. For development, using key(without quotes): "Google Analytics ID (Debug)"; For release, using key(without quotes): "Google Analytics ID (Release)".
2. Build the framework
3. Add the framework as a reference to your app's project

Expand Down