如何查找哪些GTM正在使用我的GA属性MEASUREMENT ID
批量查找使用特定GA Measurement ID的Google Tag Manager容器
方法1:通过Google Tag Manager API批量检索
- 前提:拥有目标GTM容器的只读/编辑权限,且已在Google Cloud Console启用Tag Manager API
- 核心思路:调用API遍历所有有权限访问的GTM容器,扫描标签配置中的参数,匹配目标Measurement ID(格式为
G-XXXXXXX) - 示例Python脚本(需提前安装
google-api-python-client和google-auth库):from googleapiclient.discovery import build from google.oauth2 import service_account # 替换为你的服务账号密钥路径和目标Measurement ID SERVICE_ACCOUNT_FILE = 'your-service-account-key.json' TARGET_MEASUREMENT_ID = 'G-XXXXXXX' # 初始化API客户端 credentials = service_account.Credentials.from_service_account_file( SERVICE_ACCOUNT_FILE, scopes=['https://www.googleapis.com/auth/tagmanager.readonly'] ) gtm_service = build('tagmanager', 'v2', credentials=credentials) # 遍历所有账号及容器 accounts = gtm_service.accounts().list().execute() for account in accounts.get('account', []): containers = gtm_service.accounts().containers().list(accountId=account['accountId']).execute() for container in containers.get('container', []): # 获取容器内所有标签 tags = gtm_service.accounts().containers().tags().list( accountId=account['accountId'], containerId=container['containerId'] ).execute() # 检查每个标签的参数 for tag in tags.get('tag', []): for param in tag.get('parameter', []): if param.get('value') == TARGET_MEASUREMENT_ID: print(f"匹配容器:{container['name']}(ID:{container['containerId']}),对应标签:{tag['name']}") - 运行脚本后,终端会直接输出所有包含目标Measurement ID的GTM容器和标签信息
方法2:用Google Sheets + Apps Script可视化排查
适合不熟悉代码的用户:
- 新建Google Sheet,命名为「GTM Measurement ID排查表」
- 打开「扩展程序」→「Apps Script」,替换默认代码为以下内容:
const TARGET_MEASUREMENT_ID = 'G-XXXXXXX'; // 替换为你的目标Measurement ID function scanGTMContainers() { const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); sheet.clearContents(); // 写入表头 sheet.appendRow(['GTM账号名称', '账号ID', '容器名称', '容器ID', '关联标签名称']); const gtmService = TagManagerApp.getTagManagerService(); const accounts = gtmService.listAccounts(); // 遍历所有账号、容器、标签 accounts.forEach(account => { const containers = gtmService.listContainers(account.accountId); containers.forEach(container => { const tags = gtmService.listTags(account.accountId, container.containerId); tags.forEach(tag => { tag.parameters.forEach(param => { if (param.value === TARGET_MEASUREMENT_ID) { sheet.appendRow([ account.name, account.accountId, container.name, container.containerId, tag.name ]); } }); }); }); }); } - 保存脚本后点击「运行」,完成权限授权(需拥有GTM容器的只读权限)
- 运行结束后,Sheet中会自动填充所有匹配的GTM容器及标签数据
额外提示
- 两种方法都依赖你对目标GTM容器的权限,无权限的容器无法被扫描到
- 清理完重复的Measurement ID配置后,GA的DebugView会在5-10分钟内恢复正常
内容的提问来源于stack exchange,提问作者it3xl
相关产品推荐
相关产品推荐

