內容安全政策 (CSP) 是廣泛支援的網路安全標準,可讓開發人員控管應用程式載入的資源,藉此防範特定類型的注入攻擊。您可以透過這份指南,瞭解如何在使用 CSP 的網站上部署 Google 代碼管理工具。
啟用容器代碼,以便使用 CSP
如要在含有 CSP 的網頁上使用 Google 代碼管理工具,CSP 必須允許執行代碼管理工具容器程式碼。這個程式碼會建構為內嵌 JavaScript 程式碼,用於插入 gtm.js
指令碼。您可以透過多種方式執行這項操作,例如使用 Nonce 或雜湊。建議您使用隨機值,這應該是伺服器為每個回應個別產生的隨機值,且不易被猜測。請在 Content-Security-Policy script-src
指令中提供 Nonce 值:
Content-Security-Policy:
script-src 'nonce-{SERVER-GENERATED-NONCE}';
img-src www.googletagmanager.com;
connect-src www.googletagmanager.com
接著,請使用 nonce 感知版本的內嵌代碼管理工具容器程式碼。將內嵌指令碼元素的 nonce 屬性設為相同的值:
<!-- Google Tag Manager -->
<script nonce='{SERVER-GENERATED-NONCE}'>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;var n=d.querySelector('[nonce]');
n&&j.setAttribute('nonce',n.nonce||n.getAttribute('nonce'));f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXX');</script>
<!-- End Google Tag Manager -->
接著,代碼管理工具會將 Nonce 傳播至其新增至網頁的所有指令碼。
您也可以透過其他方式啟用內嵌指令碼的執行作業,例如在 CSP 中提供內嵌指令碼的雜湊值。
如果建議的 Nonce 或雜湊方法不可行,您可以將 'unsafe-inline'
指示新增至 CSP 的 script-src
區段,啟用代碼管理工具內嵌指令碼。
如要使用這種方法,CSP 中必須包含下列指令:
指令 | 內容 |
---|---|
script-src | 'unsafe-inline' https://www.googletagmanager.com |
img-src | www.googletagmanager.com |
connect-src | www.googletagmanager.com |
自訂 JavaScript 變數
由於 自訂 JavaScript 變數的實作方式,在 CSP 存在的情況下會評估為 undefined
,除非在 CSP 的 script-src
部分提供 'unsafe-eval'
指令。
指令 | 內容 |
---|---|
script-src | 'unsafe-eval' |
預覽模式
如要使用 Google 代碼管理工具的預覽模式,CSP 必須包含下列指令:
指令 | 內容 |
---|---|
script-src | https://googletagmanager.com https://tagmanager.google.com |
style-src | https://googletagmanager.com https://tagmanager.google.com https://fonts.googleapis.com |
img-src | https://googletagmanager.com https://ssl.gstatic.com https://www.gstatic.com |
font-src | https://fonts.gstatic.com 資料: |
Google Analytics 4 (Google Analytics)
如要使用 Google Analytics 4 (Google Analytics) 代碼,CSP 必須包含下列指令:
指令 | 內容 |
---|---|
script-src | https://*.googletagmanager.com |
img-src | https://*.google-analytics.com https://*.googletagmanager.com |
connect-src | https://*.google-analytics.com https://*.analytics.google.com https://*.googletagmanager.com |
如果是使用 Google 信號的 Google Analytics 4 (Google Analytics) 部署作業,CSP 必須包含下列指令:
指令 | 內容 |
---|---|
script-src | https://*.googletagmanager.com |
img-src | https://*.google-analytics.com https://*.analytics.google.com https://*.googletagmanager.com https://*.g.doubleclick.net https://*.google.com https://*.google.<TLD> |
connect-src | https://*.google-analytics.com https://*.analytics.google.com https://*.googletagmanager.com https://*.g.doubleclick.net https://*.google.com https://*.google.<TLD> |
frame-src | https://td.doubleclick.net https://www.googletagmanager.com |
Google Ads
如要使用 Google Ads 轉換、再行銷或轉換連接器代碼,CSP 必須包含下列指令:
指令 | 內容 |
---|---|
script-src | https://www.googleadservices.com https://www.google.com https://www.googletagmanager.com https://pagead2.googlesyndication.com https://googleads.g.doubleclick.net |
img-src | https://www.googletagmanager.com https://googleads.g.doubleclick.net https://www.google.com https://pagead2.googlesyndication.com https://www.googleadservices.com https://google.com https://www.google.com.<TLD> |
frame-src | https://www.googletagmanager.com https://td.doubleclick.net |
connect-src | https://pagead2.googlesyndication.com https://www.googleadservices.com https://www.google.com https://google.com |
Google Ads 使用者資料信標
如要在安全內容中使用 Google Ads 使用者資料信標,CSP 必須包含下列指令:
指令 | 內容 |
---|---|
script-src | https://www.googletagmanager.com |
frame-src | https://www.googletagmanager.com |
connect-src | https://google.com https://www.google.com |
Google Ads 使用者資料信標不會在不安全的情況下執行,因此在這些情況下不適用 CSP 設定。
泛光燈
Floodlight 使用者可以使用下列設定啟用 CSP。將 <FLOODLIGHT-CONFIG-ID>
值替換為特定的 Floodlight 廣告主 ID,或 *
以允許任何廣告主 ID:
適用於所有使用者:
指令 | 內容 |
---|---|
img-src | https://ad.doubleclick.net https://ade.googlesyndication.com https://adservice.google.com https://www.googletagmanager.com |
frame-src | https://td.doubleclick.net https://www.googletagmanager.com |
connect-src | https://pagead2.googlesyndication.com https://www.google.com https://www.googleadservices.com |
如果是「自訂指令碼」信標:
指令 | 內容 |
---|---|
frame-src | https://<FLOODLIGHT-CONFIG-ID>.fls.doubleclick.net |
如為圖片代碼:
指令 | 內容 |
---|---|
img-src | https://ad.doubleclick.net https://ade.googlesyndication.com |
Service Worker
如要使用 Service Worker 進行強化比對、使用者資料信標和廣告轉換,CSP 必須包含下列指令:
指令 | 內容 |
---|---|
frame-src | https://www.googletagmanager.com |