使用 Google 追踪标签管理器记录 Branch 事件
概述
用例 :使用 Google 追踪代码管理器 (Google Tag Manager),Firebase 和 Branch library 记录 Branch 事件。这是使用 GTM 自定义功能完成的。
先决条件 :Branch SDK 必须在 iOS 和 Android App 内。
警告
This has not been tested intensively (with different use cases)!
在 Android 上设置自定义标签
先决条件
请确保您在 Android 上安装了 Branch SDK。
- 导入您的 GTM container
- Implement a class that extends
com.google.android.gms.tagmanager.CustomTagProvider
.
Please see an example below :
import android.support.annotation.Keep;
import java.util.Map;
import branch
@Keep
public class HighScoreProvider implements com.google.android.gms.tagmanager.CustomTagProvider {
@Override
...
}
- If using ProGuard, make sure that the class names and methods are not obfuscated. Use the Keep annotation to specify this.
- 在此自定义类中,使用下面的以下 function
public abstract void execute (Map<String, Object> parameters)
- Within this function, read the parameters passed by GTM within the "execute" function and log a Branch standard /custom event. Please ensure you import the Branch library.
@Keep
public class TagProviderImpl implements CustomTagProvider {
...
@Keep
public void execute(Map<String, Object> variables) {
BranchEvent event = new BranchEvent("View Event");
for (String key: variables.keySet()) {
event.addCustomDataProperty(key, variables.get(key).toString());
}
event.logEvent(context);
}
}
在 iOS 上设置自定义标签
先决条件
请确保您在 iOS 上安装了 Branch SDK。
- 导入您的 GTM container
- 确保您包含了 bridging header(尤其是对于同时使用 Swift 和 Objective-C 的代码库)
- 打开 Xcode。
- 点击 File > New > File。
- 在 iOS > Source下,选择 Header File。
- 点击 Next。
- Enter the header file name
BridgingHeader.h
。

6. Click Create.
- 在 build settings 上添加 Objective-C bridging header
- 在 Xcod e中,点击您的项目。

2. Click Build Settings in the editor area.
3. Select All and Combined and search for bridging.
4. In the right column of the row containing Objective-C Bridging Header, enter `BridgingHeader.h`.

- 实现一个 custom class,例如 CustomEventTagFunction
import Foundation
import GoogleTagManager
@objectivec(CustomEventTagFunction)
final class CustomEventTagFunction : NSObject, TAGCustomFunction {
...
}
- 在此 custom class 中,使用下面的以下 function:
@objectivec func execute(withParameters parameters: [AnyHashable : Any]!) -> NSObject! { ...}
- Within this function, read the parameters passed by GTM within the "execute" function and log a Branch standard or custom event. Please ensure you import the Branch library. This is an example:
import Foundation
import GoogleTagManager
import Branch
@objectivec(CustomEventTagFunction)
final class CustomEventTagFunction : NSObject, TAGCustomFunction {
@objectivec func execute(withParameters parameters: [AnyHashable : Any]!) -> NSObject! {
print("********************* parameters: \(String(describing: parameters))")
let event_Name = parameters["event_name"] as! String
let event = BranchEvent.customEvent(withName: event_Name)
parameters.keys.forEach { key in
let a = parameters[key] as! String
event.customData[key] = a
print("*", a)
}
event.logEvent()
return nil
}
}
通过 Firebase 事件触发事件
- 确保您有通过 GTM 触发的事件。
示例:
dataLayer.push(["event": "branch_event", "screenName": "Home Screen"])
设置 GTM 配置
- 为您的事件设置触发器。
- Add filters for when this trigger is fired.

- Add relevant event variables of what to add in.
- 创建一个新变量
- 选择 ‘Event Parameter’
- 与 Branch 销售工程师或解决方案工程师一起确定要创建的变量。

- Create a new Tag under the Tags section and provide a name.
- Click on Tag Configuration, and under Tag type, select Function Call.
- 在 Class path 部分下提供创建的 classpath
- iOS:请看本节
- Android:请看本节
- Expand the “Add Argument” section and provide the key as "event_name" with the event name macro. Add in other keys which you need.
- 点击触发部分,然后选择您要触发此代码的适当触发器。

- 请确保分别在 iOS 和 Android 上更新 container。
Updated 4 months ago