Skip to content

Commit

Permalink
Support limited "custom logging" from the iOS SDK
Browse files Browse the repository at this point in the history
Summary: Add support for very specific event logging in the iOS SDK.  Single events, now from FriendPicker launch and PlacePicker launch, get sent to the server.  Underlying support for apps that are not FB auth'd as well.

Test Plan: Tested via HelloFacebook

Reviewers: jacl, clang

Reviewed By: jacl

CC: msdkexp@

Differential Revision: https://phabricator.fb.com/D583180
  • Loading branch information
gregschechter committed Sep 25, 2012
1 parent 530bdc3 commit bbb0b53
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/FBFriendPickerViewController.m
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/FBPlacePickerViewController.m
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/FBUtility.h
Expand Up @@ -17,6 +17,8 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@class FBSession;

@protocol FBGraphObject;

@interface FBUtility : NSObject
Expand All @@ -38,6 +40,9 @@
withDefault:(NSString *)value
inBundle:(NSBundle *)bundle;

+ (void)logInsightsEvent:(NSString *)eventName
session:(FBSession *)session;

@end

#define FBConditionalLog(condition, desc, ...) \
Expand Down
106 changes: 105 additions & 1 deletion src/FBUtility.m
Expand Up @@ -15,12 +15,22 @@
*/

#import "FBUtility.h"
#import "FBGraphObject.h"
#import "FBRequest.h"
#import "FBSBJSON.h"
#import "FBSession.h"
#include <sys/time.h>
#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 {

Expand Down Expand Up @@ -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

0 comments on commit bbb0b53

Please sign in to comment.