只要指令出現在 Google 代碼程式碼片段下方,您就可以在網頁上的任何位置叫用 gtag() 指令。如要瞭解如何在網頁中加入程式碼片段,請參閱安裝指南。
參數範圍
您可以將參數值範圍設為個別事件、傳送至特定 <TARGET_ID> 的所有事件,或所有事件。方法是使用 event、config 和 set 指令。
在一個範圍中設定的參數值不會修改在不同範圍中為相同參數設定的值。在下列範例中,config 指令不會修改先前以 set 指令指派的 campaign_id 全域值。執行這兩個指令後,campaign_id 的全域值仍為 '1234'。
// Set global campaign IDgtag('set',{'campaign_id':'1234'});// Set campaign ID for <TARGET_ID>gtag('config','<TARGET_ID>',{'campaign_id':'ABCD'});
參數優先順序
如果不同範圍中的相同參數獲派不同值,系統在處理事件時只會使用單一值。範圍限定為 event 的參數值優先於範圍限定為 config 的參數,而 config 參數優先於使用 set 全域範圍限定的參數。
// Set campaign information at the global scopegtag('set',{'campaign_name':'Black Friday Sale'});// Set currency for <TARGET_ID1> to 'USD'gtag('config','<TARGET_ID1>',{'currency':'USD'});// Process a conversion event with currency: 'GBP'gtag('event','conversion',{'currency':'GBP','send_to':'<TARGET_ID1>'});// Process a conversion event with currency: 'EUR'gtag('event','conversion');// Process a conversion event with currency: 'USD'gtag('event','conversion',{'send_to':'<TARGET_ID1>'});
config
可讓您在目標中加入其他設定資訊。這通常是產品專屬的產品設定,但如果您同時使用 Google Ads 和 Google Analytics,只需要設定一次即可。
constgclidPromise=newPromise(resolve=>{gtag('get','DC-XXXXXXXX','gclid',resolve)});gclidPromise.then((gclid)=>{// Do something with gclid...})
將事件傳送至 Measurement Protocol
gtag('get','G-XXXXXXXXXX','client_id',(clientID)=>{sendOfflineEvent(clientID,"tutorial_begin")});functionsendOfflineEvent(clientID,eventName,eventData){// Send necessary data to your server...}
取得您設定的值
gtag('set',{campaign_name:'Spring_Sale'});gtag('get','G-XXXXXXXXXX','campaign_name',(campaign_name)=>{// Do something with currency value you set earlier.})
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-13 (世界標準時間)。"],[[["\u003cp\u003eThe Google tag (gtag.js) API uses the \u003ccode\u003egtag()\u003c/code\u003e function to send data to Google products like Google Analytics and Google Ads.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003egtag()\u003c/code\u003e function uses commands like \u003ccode\u003econfig\u003c/code\u003e, \u003ccode\u003eget\u003c/code\u003e, \u003ccode\u003eset\u003c/code\u003e, \u003ccode\u003eevent\u003c/code\u003e, and \u003ccode\u003econsent\u003c/code\u003e to manage data and user consent.\u003c/p\u003e\n"],["\u003cp\u003eParameter values can have different scopes: \u003ccode\u003eevent\u003c/code\u003e, \u003ccode\u003econfig\u003c/code\u003e, and \u003ccode\u003eset\u003c/code\u003e, with \u003ccode\u003eevent\u003c/code\u003e having the highest precedence.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003econfig\u003c/code\u003e command is used to configure targets like Google Analytics properties or Google Ads accounts.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eevent\u003c/code\u003e command sends event data, including recommended and custom events with associated parameters.\u003c/p\u003e\n"]]],["The `gtag()` API manages data sending and configuration through commands like `config`, `get`, `set`, `event`, and `consent`. `config` sets target-specific configurations, while `get` retrieves values like `client_id`. `set` defines global parameters for all subsequent events, and `event` sends event data with optional parameters. `consent` manages user consent settings, allowing to configure or update user consent choices. Parameters can be scoped to an event, a target, or globally, with event-level parameters taking precedence.\n"],null,["# Google tag API reference\n\nThe Google tag (gtag.js) API consists of a single function, `gtag()`, with\nthe following syntax: \n\n gtag(\u003ccommand\u003e, \u003ccommand parameters\u003e);\n\n- `\u003ccommand\u003e` is one of the following commands:\n - [`config`](#config)\n - [`get`](#get)\n - [`set`](#set)\n - [`event`](#event)\n - [`consent`](#consent)\n- `\u003ccommand parameters\u003e` are the parameters you can pass to `gtag()`. Command parameters vary according to the command; refer to the command reference, below.\n\nYou can invoke `gtag()` commands anywhere on your page, as long as your commands\nappear below the Google tag snippet. To learn how to add the snippet to a\npage, see [the installation guide](/tag-platform/gtagjs/install).\n\nParameter scope\n---------------\n\nYou can scope parameters values to individual events, all events sent to a\nspecific `\u003cTARGET_ID\u003e` , or globally to all events. This is achieved by using\nthe `event`, `config`, and `set` commands.\n\nParameter values set in one scope don't modify the values set for the same\nparameter in a different scope. In the example below, the `config` command does\nnot modify the global value for `campaign_id` previously assigned with the `set`\ncommand. After both commands are executed, the global value of `campaign_id` is\nstill `'1234'`. \n\n // Set global campaign ID\n gtag('set', { 'campaign_id': '1234' });\n\n // Set campaign ID for \u003cTARGET_ID\u003e\n gtag('config','\u003cTARGET_ID\u003e', { 'campaign_id': 'ABCD' });\n\nParameter precedence\n--------------------\n\nIf different values are assigned to the same parameter in different scopes, only\na single value is used when processing events. Parameter values scoped to\n`event` will take precedence over parameters scoped to `config`, and `config`\nparameters take precedence over parameters that are globally scoped using `set`. \n\n // Set campaign information at the global scope\n gtag('set', { 'campaign_name': 'Black Friday Sale' });\n\n // Set currency for \u003cTARGET_ID1\u003e to 'USD'\n gtag('config','\u003cTARGET_ID1\u003e', { 'currency': 'USD' });\n\n // Process a conversion event with currency: 'GBP'\n gtag('event','conversion', { 'currency': 'GBP', 'send_to': '\u003cTARGET_ID1\u003e' });\n\n // Process a conversion event with currency: 'EUR'\n gtag('event','conversion');\n\n // Process a conversion event with currency: 'USD'\n gtag('event','conversion', { 'send_to': '\u003cTARGET_ID1\u003e' });\n\n`config`\n--------\n\nAllows you to add additional configuration information to targets. This is\ntypically a product-specific configuration for a product, but you only need to\nconfigure this once if you're using both Google Ads and Google Analytics. \n\n gtag('config', '\u003cTARGET_ID\u003e', {\u003cadditional_config_info\u003e});\n\n`\u003cTARGET_ID\u003e` is an identifier that uniquely identifies the target for hits,\nsuch as a Google Analytics property or a Google Ads account.\n`\u003cadditional_config_info\u003e` is one or more parameter-value pairs.\n\nThis example configures a tag to send data to a Google Ads account: \n\n gtag('config', 'TAG_ID');\n\nwhere \"TAG_ID\" is the [tag ID](https://support.google.com/tagmanager/answer/12326985) for the Google tag.\n\nTo demonstrate how to send additional config information, here is an example\nthat configures a tag to send data to an Analytics account with a\n`send_page_view` parameter that passes a value of `false`, and a `groups`\nparameter that passes a value of `'agency'`. \n\n gtag('config', 'TAG_ID', {\n 'send_page_view': false,\n 'groups': 'agency'\n });\n\n`get`\n-----\n\nAllows you to get various values from `gtag.js` including values set with the\n[`set`](#set) command. \n\n gtag('get', '\u003ctarget\u003e', '\u003cfield_name\u003e', callback)\n\n| Argument | Type | Example | Description |\n|----------------|------------------------------|---------------------------------|------------------------------------------------------------------------------------------|\n| \\\u003ctarget\\\u003e | `string` | G-XXXXXXXXXX | The target to fetch values from. |\n| \\\u003cfield_name\\\u003e | [FieldName](#get_field_name) | client_id | The name of the field to get. |\n| callback | `Function` | `(field) =\u003e console.log(field)` | A function that will be invoked with the requested field, or `undefined` if it is unset. |\n\n**FieldName**\n\nField name can be the name of a custom field you set with the `gtag('set')`\ncommand, or one of the following values:\n\n| Field Name | Supported Targets |\n|----------------|---------------------------|\n| client_id | - Google Analytics 4 |\n| session_id | - Google Analytics 4 |\n| session_number | - Google Analytics 4 |\n| gclid | - Google Ads - Floodlight |\n\n| **Note:** If you use the [`set`](#set) command with these field names, the value you set will have precedence.\n\n### Examples\n\n**Get value into a Promise** \n\n const gclidPromise = new Promise(resolve =\u003e {\n gtag('get', 'DC-XXXXXXXX', 'gclid', resolve)\n });\n\n gclidPromise.then((gclid) =\u003e {\n // Do something with gclid...\n })\n\n**Send event to the Measurement Protocol** \n\n gtag('get', 'G-XXXXXXXXXX', 'client_id', (clientID) =\u003e {\n sendOfflineEvent(clientID, \"tutorial_begin\")\n });\n\n function sendOfflineEvent(clientID, eventName, eventData) {\n // Send necessary data to your server...\n }\n\n**Get a value you set** \n\n gtag('set', {campaign_name: 'Spring_Sale'});\n\n gtag('get', 'G-XXXXXXXXXX', 'campaign_name', (campaign_name) =\u003e {\n // Do something with currency value you set earlier.\n })\n\n`set`\n-----\n\nThe set command lets you define parameters that will be associated with every\nsubsequent event on the page.\n**Note:** We recommend using [`config`](#config) or [`event`](#event) commands wherever possible. \n\n gtag('set', {\u003cparameter-value-pair\u003e, \u003cparameter-value-pair\u003e});\n\nFor example, you can share [campaign](https://developers.google.com/analytics/devguides/collection/ga4/reference/config#campaign)\nparameters so that they can be accessed by multiple tags on the same page.\n\nThe example below illustrates setting a campaign name and ID for a black Friday\nshopping event. Because you've used `set`, all other tags, for example,\nGA4 Event tags or Google Ads Remarketing tags, can access this data. \n\n gtag('set', 'campaign', {\n 'id': 'abc',\n 'source': 'google',\n 'name': 'black_friday_promotion',\n 'term': 'running+shoes',\n });\n\n`event`\n-------\n\nUse the `event` command to send event data. \n\n gtag('event', '\u003cevent_name\u003e', {\u003cevent_params\u003e});\n\n`\u003cevent_name\u003e` is either:\n\n- A [recommended event](/tag-platform/gtagjs/reference/events). Each recommended event can take [recommended parameters](/tag-platform/gtagjs/reference/parameters).\n- A custom event. A custom event is an arbitrary event name that you make up, with arbitrary parameters. For more information, see [Set up events](/analytics/devguides/collection/ga4/events).\n\n`\u003cevent_params\u003e` is one or more parameter-value pairs. Each pair separated by a\ncomma.\n\nThe following `event` command fires the recommended event `screen_view` with two\nparameters: `app_name` and `screen_name`. \n\n gtag('event', 'screen_view', {\n 'app_name': 'myAppName',\n 'screen_name': 'Home'\n });\n\n| **Important:** The Google tag snippet must appear on the same page, above `event` commands, or your data will not be sent.\n\n`consent`\n---------\n\nUse the `consent` command to configure consent. \n\n gtag('consent', {\u003cconsent_arg\u003e}, {\u003cconsent_params\u003e});\n\nSee [consent](https://support.google.com/analytics/answer/9976101) in the help center for more information on the behavior these\nparameters configure.\n\n`\u003cconsent_arg\u003e` is one of `'default'` or `'update'`. `'default'` is used to set\nthe default consent parameters that should be used, and `'update'` is used to\nupdate these parameters once a user indicates their consent.\n\nThe following `\u003cconsent_params\u003e` are supported:\n\n| Field Name | Allowed Values | Description |\n|----------------------|---------------------------|--------------------------------------------------------------------------------------------------------------|\n| `ad_storage` | `'granted'` \\| `'denied'` | Enables storage, such as cookies (web) or device identifiers (apps), related to advertising. |\n| `ad_user_data` | `'granted'` \\| `'denied'` | Sets consent for sending user data to Google for advertising purposes. |\n| `ad_personalization` | `'granted'` \\| `'denied'` | Sets consent for personalized advertising. |\n| `analytics_storage` | `'granted'` \\| `'denied'` | Enables storage, such as cookies (web) or app identifiers (apps), related to analytics, e.g. visit duration. |\n| `wait_for_update` | any positive integer | Sets a time in milliseconds to wait for a consent update call. |"]]