進階 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 代碼管理工具的網頁介面中,使用完整的類別名稱設定代碼和變數:

除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-11-08 (世界標準時間)。
[null,null,["上次更新時間:2024-11-08 (世界標準時間)。"],[[["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"]]