您可以配置一个政策脚本,用于限制服务器端容器的权限。本页介绍如何为容器配置政策文件。以下说明假设您已熟悉跟踪代码管理器的自定义模板政策。
服务器端政策文件是一种 JavaScript 文件,它使用 gtag.js 语法来定义和注册一项或多项政策。
创建一个 JavaScript 文件,该文件会创建并注册一项或多项政策。每个政策函数均针对特定权限或所有权限进行注册。政策函数在返回 false 或引发异常时会拒绝权限请求。例如:
gtag('policy', 'all', function(container, policy, data) {
// This function will run for every permission check. Return true to
// allow the permission check. Return false or throw an exception to
// deny the permission check.
// container is the container id (e.g. GTM-ABC123)
// policy is the permission type (e.g. get_cookies)
// data is an object containing data about the permission request
// This policy applies to only one container. This check allows the
// same policy file to apply to more than one Tag Manager server
// container.
if (container !== 'GTM-ABC123') return true;
// Since this code runs on all permission checks, conditionally check
// the permission type and decide to permit or deny the permission.
switch (policy) {
// Container GTM-ABC123 can send HTTP requests. Everything else is
// prohibited.
case 'send_http':
return true;
// All other permission requests are denied.
default:
return false;
}
});
gtag('policy', 'get_cookies', function(container, policy, data) {
// This function will run for checks on the get_cookies permission.
// Deny all permission checks to read cookies except for the 'user_id'
// cookie. This check applies to all containers that load this policy
// file.
return data.name === 'user_id';
});将 JavaScript 文件托管在可公开访问的 HTTPS 网址所指向的资源中。该文件可托管在任何网络服务器上,但以下步骤介绍了如何在 Google Cloud Storage 存储分区中托管该文件。
- 转至 console.cloud.google.com,在页面顶部选择您的项目。
- 从左侧导航栏中选择存储 -> 浏览器。
- 点击创建存储分区。
- 按照相关步骤创建存储桶。对于访问权限控制,请选择精细控制。
- 点击上传文件,然后上传您的政策文件,亦即 JavaScript 文件。
- 文件上传后,请点击文件名,然后选择修改权限。
- 点击添加条目,并提供以下信息:
- 实体:Public
- 名称:allUsers
- 访问权限:Reader
- 点击保存。
- 点击
返回上一页。
- 在政策文件所在行中,点击复制网址。
按照创建或重新配置代码植入服务器中的步骤,修改代码植入服务器配置。当系统提示您输入政策网址时,请输入第 2 步中的网址。