使用 gtag.js 設定 Google 代碼
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Google 代碼是一組代碼,可新增至網站使用
Google 成效評估和廣告產品,例如 Google Ads、Google
Analytics、Campaign Manager、多媒體廣告與Video 360 和 Search Ads 360。而不是
管理不同 Google 產品的多個代碼時,您可以使用
適用於整個網站的 Google 代碼,並將代碼連結至多個目的地。
進一步瞭解 Google 代碼
本頁將說明如何使用
gtag.js 架構如果您不熟悉 JavaScript,建議您改用
Google 代碼管理工具,而非 gtag.js。
優點
評估轉換並改善廣告價值的最佳方式
Google 行銷投資是在網站的每個網頁放置代碼。變更者:
在整個網站中導入 Google 代碼後,您就能使用下列功能:
優點:
- 符合未來趨勢的轉換評估。善用最新功能
及整合推出的功能,讓你在使用時更加得心應手
,在發生變化時準確評估轉換。
- 無程式碼的代碼管理:在 Google 中啟用評估功能
而不必手動變更網頁上的標記
- 整合更簡單更簡單、更簡潔
與其他 Google 產品 (例如
Analytics 和 Google Ads。
- 更準確的轉換評估準確度:Google 代碼可助您一臂之力
在無法使用 Cookie 的情況下,提高轉換評估的準確度,
補回目前因產業因素而未觀察到的轉換。
在網站中加入 Google 代碼
透過 Google 代碼收集資料的步驟如下:
開啟您使用的 Google 產品,並存取 Google 代碼畫面。
如何查看代碼 ID:
複製 Google 代碼 ID。
在以下程式碼範例中貼上 Google 代碼 ID。加入 Google 代碼
加進每個所需網頁,緊接在 <head>
HTML 開頭標記後面的位置
。
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=TAG_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'TAG_ID');
</script>
必須使用 config
指令,才能將資料傳送至 Google 廣告產品。
您必須在網站上設定 config
指令才能使用許多功能,例如
自動收集的事件和轉換連結
進一步瞭解 config 指令
驗證 Google 代碼
如要確認代碼是否正在傳送資料,請按照下列步驟操作:
- 前往 tagassistant.google.com/。
- 按一下「Add domain」(新增網域)。
- 輸入要偵錯的網站網址 (開頭為「https://」)或「http://」
- 點選「連線」。
將 Tag Assistant 連結至網站後,請檢查 Google 代碼 ID
列於「找到 Google 代碼」標題中。如果 Tag Assistant 無法辨識
代碼,請前往
代碼管理工具說明中心
也可查看代碼涵蓋範圍摘要瞭解網站的
標記狀態
後續步驟
Google 代碼已設定完成,接著請設定 Google 產品並傳送事件資料。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[[["\u003cp\u003eThe Google tag is a versatile tag that streamlines the use of various Google products like Google Ads and Analytics, replacing the need for multiple individual tags.\u003c/p\u003e\n"],["\u003cp\u003eImplementing the Google tag offers benefits like future-proof conversion measurement, codeless tag management, easier integration with other Google products, and more accurate conversion tracking.\u003c/p\u003e\n"],["\u003cp\u003eTo use the Google tag, copy your tag ID from a Google product (e.g., Google Ads, Analytics), paste it into the provided code snippet, and place the snippet on every page you want to track.\u003c/p\u003e\n"],["\u003cp\u003eVerify your Google tag implementation using Tag Assistant to ensure it's sending data correctly by checking for your tag ID and consulting the Tag Manager help center for troubleshooting.\u003c/p\u003e\n"]]],["The Google tag, a single tag for Google measurement and advertising products, can be implemented across an entire website via the gtag.js framework. To set up, users need to locate their Google tag ID within Google Ads, Analytics, or Floodlight, copy it, and paste it into the provided code snippet within the `\u003chead\u003e` of each web page. Verification involves using Tag Assistant to confirm tag detection and to ensure data is being sent. Once confirmed, the tag must be configured with the config command.\n"],null,["# Set up the Google tag with gtag.js\n\nThe Google tag is a single tag you can add to your website to use\nGoogle measurement and advertising products like Google Ads, Google\nAnalytics, Campaign Manager, Display \\& Video 360, and Search Ads 360. Instead of\nmanaging multiple tags for different Google products, you can use the\nGoogle tag across your entire site and connect the tag to multiple destinations.\n\n[Learn more about the Google tag](https://support.google.com/tagmanager/answer/11994839)\n\nThis page explains how to set up and deploy a Google tag on your site using\nthe gtag.js framework. If you're unfamiliar with javascript, we recommend using\n[Google Tag Manager](/tag-platform/tag-manager) instead of gtag.js.\n\nBenefits\n--------\n\nThe best way to measure conversions and optimize the value of your\nGoogle marketing investment is to place tags on every page of your site. By\nimplementing the Google tag across your site, you gain access to the following\nbenefits:\n\n- **Future-proof conversion measurement.** Benefit from the latest features and integrations as they become available so that you are better equipped to continue measuring conversions accurately if changes occur.\n- **Codeless tag management.** Turn on measurement features within Google products without having to manually change the tags on your page.\n- **Faster and easier integration.** Streamlined, simpler implementation and integration with other Google products, such as Analytics and Google Ads.\n- **More accurate conversion measurement.** The Google tag helps you achieve more accurate conversion measurement when cookies are not available and recover conversions not observed today due to industry factors.\n\nAdd the Google tag to your website\n----------------------------------\n\nTo collect data with the Google tag:\n\n1. Open a Google product you use and access the Google tag screen.\n How to find your tag ID in:\n\n - [Google Ads](https://ads.google.com/aw/tagsettings)\n - [Google Analytics](https://support.google.com/analytics/answer/9539598)\n - [Google Floodlight](https://support.google.com/searchads/answer/2466080)\n\n \u003cbr /\u003e\n\n2. Copy your Google tag ID.\n\n3. Paste your Google tag ID in the following code sample. Place the Google tag\n snippet immediately after the opening `\u003chead\u003e` HTML tag on every page you want\n to measure.\n\n \\\u003c!-- Google tag (gtag.js) --\\\u003e\n \\\u003cscript async src=\"https://www.googletagmanager.com/gtag/js?id=\u003cvar translate=\"no\"\u003eTAG_ID\u003c/var\u003e\"\\\u003e\\\u003c/script\\\u003e\n \\\u003cscript\\\u003e\n window.dataLayer = window.dataLayer \\|\\| \\[\\];\n function gtag(){dataLayer.push(arguments);}\n gtag('js', new Date());\n gtag('config', '\u003cvar translate=\"no\"\u003eTAG_ID\u003c/var\u003e');\n \\\u003c/script\\\u003e\n\n The `config` command is needed to send data to Google advertisement products.\n Many features require the `config` command to be set on your website, such as\n automatically collected events and conversion linking.\n [Learn more about the config command](https://developers.google.com/tag-platform/gtagjs/reference#config)\n\nVerify the Google tag\n---------------------\n\nTo verify that your tag is sending data:\n\n1. Go to [tagassistant.google.com/](https://tagassistant.google.com/).\n2. Click **Add domain**.\n3. Enter the URL of the website you want to debug, starting with \"https://\" or \"http://\".\n4. Click **Connect**.\n\nWhen you connect Tag Assistant to your website, check if your Google tag ID\nappears in the header **Google tags found** . If Tag Assistant doesn't recognize\nyour tag, find debug tips in the\n[Tag Manager help center](https://support.google.com/tagassistant/answer/10039345)\n\nYou can also access the [tag coverage summary](https://support.google.com/tagmanager/answer/12270036) for an overview of your site's\ntagging status.\n\nNext steps\n----------\n\nNow that you've set up your Google tag, [configure Google products and send event data](/tag-platform/gtagjs/configure)."]]