diff --git a/src/FBFriendPickerViewController.m b/src/FBFriendPickerViewController.m index 274044abd0..3714635b8b 100644 --- a/src/FBFriendPickerViewController.m +++ b/src/FBFriendPickerViewController.m @@ -230,6 +230,8 @@ - (void)viewDidLoad self.tableView.delegate = self.selectionManager; [self.dataSource bindTableView:self.tableView]; self.loader.tableView = self.tableView; + + [FBUtility logInsightsEvent:@"_friendPickerLaunch" session:_session]; } - (void)viewDidUnload { diff --git a/src/FBPlacePickerViewController.m b/src/FBPlacePickerViewController.m index 9fc126ee99..b1a5b18682 100644 --- a/src/FBPlacePickerViewController.m +++ b/src/FBPlacePickerViewController.m @@ -298,6 +298,8 @@ - (void)viewDidLoad self.tableView.delegate = self.selectionManager; [self.dataSource bindTableView:self.tableView]; self.loader.tableView = self.tableView; + + [FBUtility logInsightsEvent:@"_placePickerLaunch" session:_session]; } - (void)viewDidUnload diff --git a/src/FBUtility.h b/src/FBUtility.h index 23dc5fc52e..85027eb0d6 100644 --- a/src/FBUtility.h +++ b/src/FBUtility.h @@ -17,6 +17,8 @@ #import #import +@class FBSession; + @protocol FBGraphObject; @interface FBUtility : NSObject @@ -38,6 +40,9 @@ withDefault:(NSString *)value inBundle:(NSBundle *)bundle; ++ (void)logInsightsEvent:(NSString *)eventName + session:(FBSession *)session; + @end #define FBConditionalLog(condition, desc, ...) \ diff --git a/src/FBUtility.m b/src/FBUtility.m index bd0ce62422..a5da6bf2b6 100644 --- a/src/FBUtility.m +++ b/src/FBUtility.m @@ -15,12 +15,22 @@ */ #import "FBUtility.h" +#import "FBGraphObject.h" +#import "FBRequest.h" +#import "FBSBJSON.h" #import "FBSession.h" #include -#import "FBGraphObject.h" @implementation FBUtility +typedef enum { + AttributionIDSendNotChecked, + AttributionIDSendAllowed, + AttributionIDSendDisallowed +} AttributionIDSendStatus; + +static AttributionIDSendStatus g_attributionIDSendStatus = AttributionIDSendNotChecked; + // finishes the parsing job that NSURL starts + (NSDictionary*)dictionaryByParsingURLQueryPart:(NSString *)encodedString { @@ -143,4 +153,98 @@ + (NSString *)localizedStringForKey:(NSString *)key return result; } ++ (void)logInsightsEvent:(NSString *)eventName + session:(FBSession *)session { + + NSString *appID = [FBSession defaultAppID]; + if (!appID) { + return; + } + + // Need to check whether attribution send is needed or allowed. + if (session || g_attributionIDSendStatus != AttributionIDSendNotChecked) { + + // Have the information we need, send immediately. + [self sendInsightsEvent:eventName session:session]; + + } else { + + + NSString *pingPath = [NSString stringWithFormat:@"%@?fields=supports_attribution", appID, nil]; + FBRequest *pingRequest = [[[FBRequest alloc] initWithSession:nil graphPath:pingPath] autorelease]; + + [pingRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) { + + @try { + + if (!error && + [result respondsToSelector:@selector(objectForKey:)] && + [[result objectForKey:@"supports_attribution"] boolValue]) { + + g_attributionIDSendStatus = AttributionIDSendAllowed; + + } else { + + // Any error or malformed response winds up here. + g_attributionIDSendStatus = AttributionIDSendDisallowed; + + } + + [self sendInsightsEvent:eventName session:session]; + + } @catch (NSException *blockException) { + + NSLog(@"Failure during logInsightsEvent ping callback: '%@'", blockException.reason); + + } + } + ]; + } +} + ++ (void)sendInsightsEvent:(NSString *)eventName + session:(FBSession *)session { + + @try { + + NSString *graphPath = [NSString stringWithFormat:@"%@/activities", [FBSession defaultAppID]]; + + NSString *attributionID = @""; + if (g_attributionIDSendStatus == AttributionIDSendAllowed) { + attributionID = [[UIPasteboard pasteboardWithName:@"fb_app_attribution" create:NO] string] ?: @""; + } + + NSArray *customEvents = @[ + @{ @"_eventName" : eventName } + ]; + + NSError *myError = nil; + FBSBJSON *writer = [[FBSBJSON alloc] init]; + NSString *customEventsJSON = [writer stringWithFragment:customEvents error:&myError]; + FBConditionalLog(!myError, @"Shouldn't get a JSON encoding error"); + [writer release]; + + NSDictionary *postParameters = + @{ + @"event" : @"CUSTOM_APP_EVENTS", + @"attribution": attributionID, + @"custom_events" : customEventsJSON, + }; + + FBRequest *request = [[[FBRequest alloc] initWithSession:session + graphPath:graphPath + parameters:postParameters + HTTPMethod:@"POST"] autorelease]; + + // Fire and forget + [request startWithCompletionHandler:nil]; + + } @catch (NSException *exception) { + + NSLog(@"Failure during sendInsightsEvent: '%@'", exception.reason); + + } +} + + @end