コンテンツにスキップ


Google Tag マネージャー for iOS

SDKの初期化

Braze iOS SDKは、Google Tag マネージャーで設定されたタグによって初期化および制御することができます。

Google Tag マネージャーを使用する前に、まずSDKの初期設定を行ってください。

Google Tag マネージャーの設定

この例では、ユーザーが曲を聴いている間に別のイベントをロギングする必要がある音楽ストリーミングアプリを想定しています。Google Tag マネージャー for iOSを使用して、どのサードパーティベンダーがこのイベントを受信するかをコントロールし、Braze固有のタグを作成できます。

カスタムイベント

カスタムイベントは、logEvent に設定した actionType によってログに記録されます。この例のBrazeカスタムタグプロバイダーは、eventName を使用してカスタムイベント名を設定することを想定しています。

最初に、played song である「イベント名」を検索するトリガーを作成します。

「eventName」が「played song」である場合に一部のイベントに対してトリガーするよう設定されたGoogle Tag Managerのカスタムトリガー。

次に、新しいタグ(「Function Call」とも呼ばれます)を作成し、この記事で後述するカスタムタグプロバイダーのクラスパスを入力します。

このタグは、先ほど作成した played song イベントをロギングするとトリガーされます。

サンプルタグのカスタムパラメーター(キーと値のペア)では、eventName を played song に設定しました。これが、Brazeにロギングされるカスタムイベント名になります。

classpathフィールドと、キーと値のペアフィールドを含むGoogle Tag Managerのタグ。このタグは、以前に作成された「再生された曲」トリガーでトリガーされるように設定されています。

また、追加のキーと値のペア引数をタグに含めることもできます。この引数は、カスタムイベントプロパティとしてBrazeに送信されます。eventName および actionType は、カスタムイベントプロパティでは無視されません。次のサンプルタグでは、genre を渡します。これは、Google Tag マネージャーでタグ変数を使用して定義されており、アプリでロギングしたカスタムイベントから取得されます。

genre イベントプロパティは、「Firebase - Event Parameter」変数としてGoogle Tag マネージャーに送信されます。Google Tag マネージャー for iOSでは、Firebaseがデータレイヤーとして使用されるためです。

「genre」が「Braze - Played Song Event」タグのイベントパラメーターとして追加されるGoogle Tag Managerの変数。

最後に、ユーザーがアプリで曲を再生すると、タグのトリガー名 played song と一致するFirebase分析イベント名を使用し、FirebaseとGoogle Tag マネージャーを介してイベントがロギングされます。

NSDictionary *parameters = @{@"genre" : @"pop",
                             @"number of times listened" : @42};
[FIRAnalytics logEventWithName:@"played song" parameters:parameters];

カスタム属性のロギング

カスタム属性は、customAttribute に設定された actionType を介して設定されます。Brazeカスタムタグプロバイダーは、カスタム属性のキーと値が customAttributeKey および customAttributeValue を介して設定されることを想定しています。

NSDictionary *parameters = @{@"customAttributeKey" : @"favorite song",
                             @"customAttributeValue" : @"Private Eyes"};
[FIRAnalytics logEventWithName:@"customAttribute" parameters:parameters];

changeUserの呼び出し

changeUser() の呼び出しは、changeUser に設定された actionType を介して行われます。Brazeカスタムタグプロバイダーは、BrazeユーザーIDがタグ内のキーと値のペア externalUserId を介して設定されることを想定しています。

NSDictionary *parameters = @{@"externalUserId" : userId};
[FIRAnalytics logEventWithName:@"changeUser" parameters:parameters];

Braze SDKカスタムタグプロバイダー

タグとトリガーが設定されたら、iOSアプリにGoogle Tag マネージャーを実装する必要もあります。これについては、Googleのドキュメントに記載されています。

Google Tag マネージャーがアプリにインストールされたら、カスタムタグプロバイダーを追加し、Google Tag マネージャー内で設定したタグに基づいてBraze SDKメソッドを呼び出します。

ファイルへの「Class Path」を必ず書き留めておいてください。Google Tag マネージャーコンソールでタグを設定するときに入力する内容です。

この例は、カスタムタグプロバイダーを構築する多くの方法の1つを示しています。ここでは、GTMタグから送信されたキーと値のペア actionType に基づいて、呼び出すBraze SDKメソッドを決定します。

この例でサポートされている actionType は logEvent、customAttribute、changeUser ですが、タグプロバイダーによるGoogle Tag マネージャーからのデータの処理方法を変更することもできます。

以下のコードを BrazeGTMTagManager.h ファイルに追加します。

@import Firebase;
@import GoogleTagManager;

@interface BrazeGTMTagManager : NSObject <TAGCustomFunction>

@end

以下のコードを BrazeGTMTagManager.m ファイルに追加します。

#import <Foundation/Foundation.h>
#import "BrazeGTMTagManager.h"
#import "Appboy-iOS-SDK/AppboyKit.h"

static NSString *const ActionTypeKey = @"actionType";

// Custom Events
static NSString *const LogEventActionType = @"logEvent";
static NSString *const LogEventEventName = @"eventName";

// Custom Attributes
static NSString *const CustomAttributeActionType = @"customAttribute";
static NSString *const CustomAttributeKey = @"customAttributeKey";
static NSString *const CustomAttributeValueKey = @"customAttributeValue";

// Change User
static NSString *const ChangeUserActionType = @"changeUser";
static NSString *const ChangeUserExternalUserId = @"externalUserId";

@implementation BrazeGTMTagManager

- (NSObject *)executeWithParameters:(NSDictionary *)parameters {
  NSMutableDictionary *mutableParameters = [parameters mutableCopy];

  NSString *actionType = mutableParameters[ActionTypeKey];
  if (!actionType) {
    NSLog(@"There is no Braze action type key in this call. Doing nothing.", nil);
    return nil;
  }

  [mutableParameters removeObjectForKey:ActionTypeKey];

  if ([actionType isEqualToString:LogEventActionType]) {
    [self logEvent:mutableParameters];
  } else if ([actionType isEqualToString:CustomAttributeActionType]) {
    [self logCustomAttribute:mutableParameters];
  } else if ([actionType isEqualToString:ChangeUserActionType]) {
    [self changeUser:mutableParameters];
  } else {
    NSLog(@"Invalid action type. Doing nothing.");
  }
  return nil;
}

- (void)logEvent:(NSMutableDictionary *)parameters {
  NSString *eventName = parameters[LogEventEventName];
  [parameters removeObjectForKey:LogEventEventName];
  [[Appboy sharedInstance] logCustomEvent:eventName withProperties:parameters];
}

- (void)logCustomAttribute:(NSMutableDictionary *)parameters {
  NSString *customAttributeKey = parameters[CustomAttributeKey];
  id customAttributeValue = parameters[CustomAttributeValueKey];

  if ([customAttributeValue isKindOfClass:[NSString class]]) {
    [[Appboy sharedInstance].user setCustomAttributeWithKey:customAttributeKey
                                             andStringValue:customAttributeValue];
  } else if ([customAttributeValue isKindOfClass:[NSDate class]]) {
    [[Appboy sharedInstance].user setCustomAttributeWithKey:customAttributeKey
                                               andDateValue:customAttributeValue];
  } else if ([customAttributeValue isKindOfClass:[NSNumber class]]) {
    if (strcmp([customAttributeValue objCType], [@(YES) objCType]) == 0) {
      [[Appboy sharedInstance].user setCustomAttributeWithKey:customAttributeKey
                                                 andBOOLValue:[(NSNumber *)customAttributeValue boolValue]];
    } else if (strcmp([customAttributeValue objCType], @encode(short)) == 0 ||
               strcmp([customAttributeValue objCType], @encode(int)) == 0 ||
               strcmp([customAttributeValue objCType], @encode(long)) == 0) {
      [[Appboy sharedInstance].user setCustomAttributeWithKey:customAttributeKey
                                              andIntegerValue:[(NSNumber *)customAttributeValue integerValue]];
    } else if (strcmp([customAttributeValue objCType], @encode(float)) == 0 ||
               strcmp([customAttributeValue objCType], @encode(double)) == 0) {
      [[Appboy sharedInstance].user setCustomAttributeWithKey:customAttributeKey
                                               andDoubleValue:[(NSNumber *)customAttributeValue doubleValue]];
    } else {
      NSLog(@"Could not map NSNumber value to Appboy custom attribute:%@", customAttributeValue);
    }
  } else if ([customAttributeValue isKindOfClass:[NSArray class]]) {
    [[Appboy sharedInstance].user setCustomAttributeArrayWithKey:customAttributeKey
                                                           array:customAttributeValue];
  }
}

- (void)changeUser:(NSMutableDictionary *)parameters {
  NSString *userId = parameters[ChangeUserExternalUserId];
  [[Appboy sharedInstance] changeUser:userId];
}

@end
New Stuff!