UIActivityViewControllerでアクション完了後にグーグルアナリティクスにトラッキングを送信するカテゴリ

#UIActivityViewControllerでのユーザーアクションをトラッキングする

細かくやろうとするといろいろ面倒だった。カテゴリ作って今後も使いまわそう。で、そのカテゴリ貼っておく。どうぞ。

##使い方

completionWithItemsHandlerをUIActivityViewControllerにぶち込むときにcompletionWithItemsHandlerAndTrackingに対してぶち込むだけでトラッキング用のコードを追加してくれるよ。

例えばこんな感じに。

```objc
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[imageView.image] applicationActivities:@[[LKLineActivity new]]];
activityViewController.completionWithItemsHandlerAndTracking = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
	if (activityError) DDLogError(@"%@", activityError);
	else if (completed) DDLogInfo(@"action completed: %@", activityType);
};
[self presentViewController:activityViewController animated:YES completion:nil];
```

LINEが追加されているのはお好みで。
Cocoapodsで入手出来るLineKitがこの前のバージョンアップでLINEのためのUIActivityのサブクラスが追加されて便利になってた。

とくにブロックの中でやることがなければ

```objc
activityViewController.completionWithItemsHandlerAndTracking = nil;
```

だけでもおｋ。

##コード

```objc:UIActivityViewController+GAITracking.h
#import <Foundation/Foundation.h>

@interface UIActivityViewController (GAITracking)

- (void)setCompletionWithItemsHandlerAndTracking:(UIActivityViewControllerCompletionWithItemsHandler)completionWithItemsHandler;

@end
```


```objc:UIActivityViewController+GAITracking.m
#import <GoogleAnalytics-iOS-SDK/GAI.h>
#import <GoogleAnalytics-iOS-SDK/GAIDictionaryBuilder.h>
#import "UIActivityViewController+GAITracking.h"

NSString *const UIActivityTypeLINE = @"org.dlackty.LKLineActivity"; // LineKit/LKLineActivity

@implementation UIActivityViewController (GAITracking)

#define CASE(str) if ([__s__ isEqualToString:(str)])
#define SWITCH(s) for (NSString *__s__ = (s); __s__; __s__ = nil)
#define DEFAULT

- (void)setCompletionWithItemsHandlerAndTracking:(UIActivityViewControllerCompletionWithItemsHandler)completionWithItemsHandler {
	self.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {

		// completionWithItemsHandler
		if (completionWithItemsHandler) completionWithItemsHandler(activityType, completed, returnedItems, activityError);

		// and tracking...
		if (activityError == nil && completed) {

			NSString *network = nil, *action = nil;

			SWITCH(activityType) {
				CASE(UIActivityTypePostToFacebook) {
					network = @"Facebook";
					action = @"Post";
					break;
				}
				CASE(UIActivityTypePostToTwitter) {
					network = @"Twitter";
					action = @"Post";
					break;
				}
				CASE(UIActivityTypePostToWeibo) {
					network = @"SinaWeibo";
					action = @"Post";
					break;
				}
				CASE(UIActivityTypeMessage) {
					network = @"Message";
					action = @"Send";
					break;
				}
				CASE(UIActivityTypeMail) {
					network = @"Mail";
					action = @"Send";
					break;
				}
				CASE(UIActivityTypePrint) {
					network = @"Print";
					action = @"Local";
					break;
				}
				CASE(UIActivityTypeCopyToPasteboard) {
					network = @"Pasteboard";
					action = @"Copy";
					break;
				}
				CASE(UIActivityTypeAssignToContact) {
					network = @"Contact";
					action = @"Assign";
					break;
				}
				CASE(UIActivityTypeSaveToCameraRoll) {
					network = @"CameraRoll";
					action = @"Save";
					break;
				}
				CASE(UIActivityTypeAddToReadingList) {
					network = @"ReadingList";
					action = @"Add";
					break;
				}
				CASE(UIActivityTypePostToFlickr) {
					network = @"Flickr";
					action = @"Post";
					break;
				}
				CASE(UIActivityTypePostToVimeo) {
					network = @"Vimeo";
					action = @"Post";
					break;
				}
				CASE(UIActivityTypePostToTencentWeibo) {
					network = @"TencentWeibo";
					action = @"Post";
					break;
				}
				CASE(UIActivityTypeAirDrop) {
					network = @"AirDrop";
					action = @"Local";
					break;
				}
				CASE(UIActivityTypeLINE) {
					network = @"LINE";
					action = @"Send";
					break;
				}
				DEFAULT{
					network = activityType;
					action = @"Other";
					break;
				}
			}
			id <GAITracker> tracker = [GAI sharedInstance].defaultTracker;
			[tracker send:[GAIDictionaryBuilder createSocialWithNetwork:network action:action target:nil].build];
		}
	};
}

@end
```