搭配內容安全政策使用代碼管理工具

內容安全政策 (CSP) 是廣受支援的網路安全標準,可讓開發人員控管應用程式載入的資源,防止特定類型的注入式攻擊。請參閱本指南,瞭解如何在採用 CSP 的網站上部署 Google 代碼管理工具。

啟用容器代碼以使用 CSP

如要在含有 CSP 的網頁上使用 Google 代碼管理工具,CSP 必須允許執行代碼管理工具容器程式碼。這段程式碼會建構為內嵌 JavaScript 程式碼,用於插入 gtm.js 指令碼。有幾種方法可以達成上述目的,例如使用隨機數或雜湊。建議使用隨機數,這應該是伺服器為每個回應個別產生的隨機值,且無法猜測。在內容安全政策 script-src 指令中提供隨機值:

Content-Security-Policy:
script-src 'nonce-{SERVER-GENERATED-NONCE}';
img-src www.googletagmanager.com;
connect-src www.googletagmanager.com www.google.com

然後使用內嵌代碼管理工具容器程式碼的 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 -->

代碼管理工具隨後會將隨機碼傳播至新增至網頁的任何指令碼。

您也可以採用其他方法,例如在 CSP 中提供內嵌指令碼的雜湊,啟用內嵌指令碼的執行作業。

如果無法採用建議的隨機數或雜湊方法,可以將 'unsafe-inline' 指令新增至 CSP 的 script-src 區段,啟用代碼管理工具內嵌指令碼。

如要使用這種方法,CSP 中必須包含下列指令:

指令 內容
script-src 'unsafe-inline' https://www.googletagmanager.com
img-src www.googletagmanager.com
connect-src www.googletagmanager.com www.google.com

自訂 JavaScript 變數

由於自訂 JavaScript 變數的導入方式,除非在 CSP 的 script-src 部分提供 'unsafe-eval' 指令,否則這些變數在 CSP 存在時會評估為 undefined

指令 內容
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 Analytics 4 (Google Analytics) 部署作業使用 Google 信號,CSP 必須包含下列指令:

指令 內容
script-src https://*.googletagmanager.com
img-src https://*.google-analytics.com https://*.googletagmanager.com
https://*.g.doubleclick.net https://*.google.com https://*.google.<TLD>
connect-src https://*.google-analytics.com https://*.googletagmanager.com
https://*.g.doubleclick.net https://*.google.com https://*.google.<TLD> https://pagead2.googlesyndication.com
frame-src https://td.doubleclick.net https://www.googletagmanager.com

如要使用 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 使用者資料信號,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
https://ad.doubleclick.net

「自訂指令碼」信號:

指令 內容
frame-src https://<FLOODLIGHT-CONFIG-ID>.fls.doubleclick.net

圖片代碼:

指令 內容
img-src https://ad.doubleclick.net https://ade.googlesyndication.com

Service Worker

如要使用 Service Worker 進行強化比對、使用者資料信號和 Google Ads 轉換,CSP 必須包含下列指令:

指令 內容
frame-src https://www.googletagmanager.com