Android 版跟踪代码管理器的高级配置
要扩展 Google 跟踪代码管理器的功能,您可以添加函数调用变量和函数调用代码。借助函数调用变量,您可以捕获预注册函数的调用所返回的值。借助函数调用代码,您可以执行预注册函数(例如,针对跟踪代码管理器中的代码模板目前不支持的其他衡量和再营销工具触发命中)。
若要通过函数调用添加自定义代码或自定义变量,请执行以下步骤:
实现一个可以扩展 com.google.android.gms.tagmanager.CustomTagProvider
或 com.google.android.gms.tagmanager.CustomVariableProvider
的类:
import android.support.annotation.Keep;
import java.util.Map;
@Keep
public class HighScoreProvider implements com.google.android.gms.tagmanager.CustomVariableProvider {
@Override
public String getValue(Map<String, Object> map) {
synchronized (HighScoreProvider.class) {
return ((Long)sHighScore).toString();
}
}
private static long sHighScore = 0;
public static void recordScore(long score) {
synchronized (HighScoreProvider.class) {
sHighScore = Math.max(score, sHighScore);
}
}
}
如果您使用的是 ProGuard,请确保不对类名称和方法进行混淆处理。使用 Keep 注释进行指定。
在 Google 跟踪代码管理器的网页界面中,使用完全限定的类名称来设置代码和变量:

如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2023-10-27。
[null,null,["最后更新时间 (UTC):2023-10-27。"],[[["Function Call variables enable capturing values from pre-registered function calls, extending Google Tag Manager's capabilities."],["Function Call tags allow execution of pre-registered functions, such as triggering hits for unsupported measurement tools."],["Custom tags and variables can be added by implementing a class extending `CustomTagProvider` or `CustomVariableProvider`."],["ProGuard users should prevent obfuscation of custom class names and methods using the Keep annotation."],["Within the Google Tag Manager interface, use the fully qualified class name to configure the custom tags and variables."]]],["Function Call variables and tags extend Google Tag Manager's functionality. Function Call variables capture values from pre-registered function calls, while Function Call tags execute pre-registered functions. To add custom tags/variables, implement a class extending `CustomTagProvider` or `CustomVariableProvider`. Use the `@Keep` annotation to prevent obfuscation with ProGuard. Finally, configure tags and variables in Google Tag Manager's web interface using the fully qualified class name.\n"]]