本文面向需要使用同一 Google 代码衡量多个网域的开发者。本文将重点介绍 gtag.js 代码实现。您还可以在您 GA4 媒体资源的 Google 代码设置中配置其他网域。
借助跨网域衡量功能,您能够将多个独立网域上的两个或多个相关网站作为一个整体进行衡量。Google 代码为支持的产品提供了一种通用方式,可以在客户转化历程跨多个网域的情况下衡量活动。跨网域衡量功能适用于所有 Google 产品,包括 Google Analytics(分析)、Google Ads 转化衡量和 Floodlight 转化。
[null,null,["最后更新时间 (UTC):2024-02-06。"],[[["\u003cp\u003eThis guide explains how to implement cross-domain measurement with Google tags (gtag.js) to track user journeys across multiple related websites as a single user.\u003c/p\u003e\n"],["\u003cp\u003eCross-domain measurement is essential when you need to consolidate data from two or more domains, such as an online store and a separate shopping cart domain.\u003c/p\u003e\n"],["\u003cp\u003eIt works by sharing first-party measurement cookies between domains using a linker parameter appended to URLs, enabling unified user tracking.\u003c/p\u003e\n"],["\u003cp\u003eSetup involves creating a \u003ccode\u003elinker\u003c/code\u003e object in gtag.js or using the Conversion Linker tag in Google Tag Manager, specifying the linked domains.\u003c/p\u003e\n"],["\u003cp\u003eGoogle Analytics 4 users should preferably configure cross-domain measurement through the Admin interface instead of directly through tags.\u003c/p\u003e\n"]]],["Cross-domain measurement combines data from multiple domains into a single user journey. Implementation involves setting up a `linker` object on the source domain to append a linker parameter (`_gl`) to URLs leading to destination domains. This allows first-party cookie sharing. In gtag.js, use `gtag('set', 'linker', {'domains': [...]})` to specify domains. Tag Manager requires configuring a Conversion Linker tag and enabling auto-linking. The destination domain must be configured to accept incoming linker parameters, either automatically or by setting `accept_incoming` to true.\n"],null,["# Measure activity across multiple domains\n\n\u003e This article is for developers who need to measure multiple domains with the\n\u003e same Google tag. This article focuses on a gtag.js code implementation. You\n\u003e can also configure additional domains in your [Google tag settings](https://support.google.com/tagmanager/answer/12131703#Domains&zippy=%2Cconfigure-your-domains)\n\u003e of your GA4 property.\n\nCross-domain measurement enables two or more related sites on separate domains\nto be measured as one. Google tags provide a common way for supported products\nto measure activity when your customer journeys span across multiple domains.\nCross-domain measurement works for all Google products, including Google\nAnalytics, Google Ads conversion measurement, and Floodlight conversions.\n\nWhen to implement cross-domain measurement\n------------------------------------------\n\nImplement cross-domain measurement when you need to combine data for two or more\ndomains.\n\nSuppose you have a website at `example-petstore.com`, and your website's\necommerce component is hosted by a third-party shopping cart on another domain:\n`example-commerce-host.com/example-petstore`.\n\n*Without* cross-domain measurement, a user who arrives to your online store and\nthen proceeds to your third-party shopping cart is counted as two separate\nusers, with two separate sessions of different durations. *With* cross-domain\nmeasurement, activity is captured as a single user.\n\nHow it works\n------------\n\nThe domain linker shares first-party measurement cookies between a source domain\nand a destination domain. The first-party measurement cookies are stored in a\nweb browser, and can only be accessed by pages on the same domain.\n\nSetup for cross-domain measurement is a two-step process:\n\n1. The source domain decorates URLs that point to the destination domain so that they contain the first-party measurement cookie values of the source domain.\n2. The destination domain checks for the presence of linked measurement cookies.\n\nCross-domain measurement with Google tags will add a linker parameter to URLs\nthat point to the destination domain. The linker parameter is identified in URL\nquery parameters with the key `_gl`: \n\n https://www.example.com/?_gl=1~abcde5~\n\nOn the destination domain, tags are configured to check for linker parameters in\nthe URL. If a valid linker parameter is found, the tag extracts the first-party\nmeasurement cookie and stores it.\n\nSet up cross-domain linking\n---------------------------\n\nTo set up cross-domain measurement on the source domain for URLs that point to\nthe destination domain, you need to create a `linker` object. The linker will\nlisten for clicks on links that point to destination domains, and it will\nautomatically add a linker parameter to the URLs of those links.\n| **Note:** Linker parameters expire after two minutes, so the linker parameter is only added at the time of the click event.\n\nThe `linker` object requires a `domains` parameter, which is an array of one or\nmore domains. This code will append the linker parameter to links on a page that\npoints to a target domain *'example.com'*: \n\n### gtag.js\n\nTo set up cross-domain measurement for the Google tag, add a `linker`\ncommand to your tag as follows. Replace `example.com` with your domain.\n**Note:** To ensure `set` values are available for `config` commands and available to any dynamically loaded containers, add the `set` commands above the `js` command. \n\n \u003cscript async src=\"https://www.googletagmanager.com/gtag/js?id=TAG_ID\"\u003e\u003c/script\u003e\n \u003cscript\u003e\n window.dataLayer = window.dataLayer || [];\n function gtag(){dataLayer.push(arguments);}\n gtag('set', 'linker', {'domains': \\['example.com'\\]});\n gtag('js', new Date());\n\n gtag('config', 'TAG_ID');\n \u003c/script\u003e\n\nYou can list multiple string values in the `domains` property. When the\n`domains` property has at least one value, gtag.js will accept incoming\ndomain links by default. You can add several values to the `domains` propety\nand use the same code snippet on every domain.\n\nGoogle Analytics 4 honors `gtag('set', 'linker', ...)` configurations, so\nlong as the `linker` command is set before the relevant `config` command.\nHowever, we recommend configuring cross-domain measurement on the Admin page\nof Google Analytics 4. [Learn how to set up cross domain\nmeasurement](https://support.google.com/analytics/answer/10071811#setup) \n\n gtag('set', 'linker', {\n 'domains': \\['example.com', 'example-b.com'\\]\n });\n\n| **Note:** The Google tag will match against any link with a hostname that contains a value found in the `domains` array.\n\n### Tag Manager\n\n### For Google Ads and Floodlight tags\n\nIn Tag Manager, create a **Conversion Linker** tag and then set the tag to\nauto-link domains.\n\n1. Click **Tags \\\u003e New**.\n2. Click **Tag Configuration** and select **Conversion Linker**.\n3. Select a trigger. In most cases, you should use a trigger that fires on all page views for cross domain measurement, or on specific conversion pages where site visitors will land after an ad is clicked.\n4. Select **Enable linking across domains**. This setting will enable this tag to handle incoming links that have a linker parameter.\n5. In **Auto Link Domains**, enter a list of domains that should be linked with this tag. Any links that point to a domain listed in this field will have a linker parameter appended to the URL.\n6. Save and publish your tag configuration.\n\n[Learn more about Tag Manager's Conversion Linker](https://support.google.com/tagmanager/answer/7549390)\n\n### For Google Analytics 4 tags\n\nIn Google Analytics 4, cross-domain measurement is configured via the Google\nAnalytics Admin interface. [Learn how to set up cross domain measurement\nwith Google Analytics 4](https://support.google.com/analytics/answer/10071811).\n| **Note:** The preferred method is to make changes to the Google Analytics Settings variable, so that changes are shared across multiple tags.\n\n1. In the **Auto Link Domains** field, enter a comma-separated list of domains.\n2. To receive incoming links from other domains, navigate to **More\n Settings \\\u003e Fields to Set** and add a field with a Field Name of *allowLinker* and a **Value** of *true*.\n3. Save your changes and publish the container.\n\nLinker parameter reference\n--------------------------\n\n| Parameter | Type | Accepted values |\n|---------------------------------------|---------|--------------------------------------------------------------------------|\n| [`accept_incoming`](#accept_incoming) | boolean | `true` (default if `domains` has value), `false` |\n| [`decorate_forms`](#decorate_forms) | boolean | `true`, **`false`** (default) |\n| [`domains`](#domains) | array | An array of one or more domains, e.g. `['example1.com', 'example2.com']` |\n| [`url_position`](#url_position) | string | `'query'` (default), `'fragment'` |\n\n### `accept_incoming`\n\nOnce a user arrives at a page on the destination domain with a linker parameter\nin the URL, your tags need to be configured to parse that parameter.\n\nIf the destination domain has been configured to automatically link domains, it\nwill accept linker parameters by default. No additional code is required on the\ndestination domain.\n\nIf the destination domain is *not* configured to automatically link domains, you\ncan instruct the destination page to look for linker parameters. Set the\n`accept_incoming` property to `true`. \n\n### gtag.js\n\n gtag('set', 'linker', {\n 'accept_incoming': true\n });\n\n### Tag Manager\n\n#### Conversion Linker tags for Google Ads and Floodlight conversions\n\nActivate the **Accept incoming linker parameters** checkbox.\n\n### `decorate_forms`\n\nTo measure form data that is sent between multiple domains, set the\n`decorate_forms` property to `true`. \n\n### gtag.js\n\n gtag('set', 'linker', {\n 'domains': ['example.com'],\n 'decorate_forms': true\n });\n\n### Tag Manager\n\n#### Conversion Linker tags for Google Ads and Floodlight conversions\n\nSet **Decorate Forms** to *true*.\n\n#### Google Analytics 4 tags\n\nThere is no control for `decorate_forms` for Google Analytics tags in Tag\nManager. Refer to the gtag.js guidance instead.\n\n### `domains`\n\nAn array of one or more domains to be linked. \n\n### gtag.js\n\n gtag('set', 'linker', {\n 'domains': \\[\n 'example1.com',\n 'example2.com',\n 'subdomain1.example3.com',\n 'subdomain2.example3.com'\n \\]\n });\n\n### Tag Manager\n\n#### Conversion Linker tags for Google Ads and Floodlight conversions\n\n1. Select *Enable linking across domains*\n2. In the Auto Link Domains field, enter a list of domains separated by commas.\n\n#### Google Analytics 4\n\nSet up cross domain measurement via the [Admin](https://support.google.com/analytics/answer/10071811) pages in Google Analytics.\n\n### `url_position`\n\nCertain content applications require you to use a fragment/hash character (`#`)\nas the delimiter in URL strings instead of the more commonly used question mark\ncharacter (`?`) to indicate query parameters. To configure the linker parameter\nto appear in the URL after a `#` character (e.g.\n`https://example.com#_gl=1~abcde5~`), set `url_position` to `fragment`. \n\n### gtag.js\n\n gtag('set', 'linker', {\n 'domains': ['example.com'],\n 'decorate_forms': true,\n 'url_position': 'fragment'\n });\n\n### Tag Manager\n\n#### Conversion Linker tags for Google Ads and Floodlight conversions\n\nIf you need to tell Tag Manager to read the unique parameter from a fragment\n(`#`) instead of a standard query (`?`), set **URL Position** to *Fragment* .\nOtherwise, leave this option set to the default *Query Parameter* option."]]