![Figure 2: Conversion Tracking Overview](https://developers.google.com/static/actions-center/images/conversion-tracking.png) **Figure 1.** Conversion tracking overview

## Overview

Conversion tracking keeps the track of conversions initiated by Google
through one of our [Action Center integrations](https://developers.google.com/actions-center.md.txt). It helps to
keep your integration healthy and running, as it can influence the
ranking on certain pages. Each time Google generates an `action_link`, the
specific URL is modified to include a unique query parameter: `rwg_token`. Store
the token along with the landing merchant identifier, and return the `rwg_token`
and `merchant_changed` values when a user completes a booking.

To complete the integration, follow these steps:

1. Parse the `rwg_token` and identify the merchant.
2. Store the `rwg_token` and `merchant_id`.
3. Return the `rwg_token` and `merchant_changed` values.
4. Test and verify the conversion tracking.

> [!NOTE]
> **Note:** The standard integration procedure requires partners to implement conversion tracking for performance measurement, with technical steps detailed in the sections below. Be advised that conversion tracking implementation is optional for partners in the [healthcare](https://developers.google.com/actions-center/verticals/appointments/redirect/policies/platform-policies.md.txt#supported_merchant_types) vertical based on business needs.

## Parse the `rwg_token` and identify the merchant

To complete the integration, you must capture both the `rwg_token` from the
Google referral URL and the identifier of the landing merchant so you can store
them together for 30 days.

### Parse the `rwg_token`

When a user is directed to your booking page, Google appends a unique
`rwg_token` query parameter to the `action_link` URL you provided in your feeds.
The `rwg_token` value is an encrypted string generated by Google that contains
metadata about the referral. Because the token is opaque to partners, you cannot
extract the merchant identifier from it.

> [!IMPORTANT]
> **Important:** No personally identifying information is stored or encrypted in the token.

In your booking page, parse the `rwg_token` value from the URL. The
following example shows how to parse the `rwg_token` in a browser for
device-level tracking:

```javascript
const rwgToken =
    new URLSearchParams(location.search).get('rwg_token') || undefined;
```

> [!IMPORTANT]
> **Important:** `URLSearchParams.get()` automatically URL-decodes the `rwg_token` value (for example, converting `%3D%3D` to `==`). Google recommends URL-encoding the token when storing it in a browser cookie (using `encodeURIComponent()`), because browser cookies assign special meaning to characters such as `=`. Make sure you send the URL-decoded token (including any trailing `=` characters) in the conversion request.

### Identify the merchant

When a user is directed to your booking page, you also need to
identify the landing merchant so you can determine later whether the user
completed a booking with the same merchant or a different merchant.

> [!IMPORTANT]
> **Important:** Google does not pass or return the `merchant_id` in the referral URL or in the `rwg_token`. Your site is responsible for identifying the merchant that the user landed on (for example, from your own `action_link` URL path, query parameters, or landing page context).

```javascript
function getLandingMerchantId() {
  // TODO: Implement this function to return a unique identifier (string) for
  // the merchant the user landed on (for example, parsed from your action link
  // URL path, query parameters, or landing page context).
}

const merchantId = getLandingMerchantId();
```

> [!NOTE]
> **Note:** If your booking page isn't within a multi-merchant platform and users can't navigate to a different merchant, you don't need to store `merchant_id` and can hardcode `merchant_changed` to `2` when sending conversion events. See [Return the merchant change value](https://developers.google.com/actions-center/verticals/local-services/redirect/integration-steps/conversion-tracking.md.txt#return-merchant).

## Store the `rwg_token` and `merchant_id`

After parsing the `rwg_token` and identifying the landing merchant, store both
values together. You can implement conversion tracking storage at two different
levels:

- Device-level
- User-level

You can store the values at either level, but **you must store both the
`rwg_token` and `merchant_id` for 30 days after the initial referral**.

The following example shows device-level storage in a browser using first-party
cookies. It assumes you parsed `rwgToken` and identified `merchantId` as shown
in the previous section. Each cookie must be written in its own
`document.cookie` assignment. Make sure to update
`rootdomain.com` with your domain.

```javascript
// Store rwgToken and merchantId in separate first-party cookies with a 30-day
// expiration (2592000 seconds).
if (rwgToken !== undefined) {
  const cookieAttrs =
      '; max-age=2592000; domain=rootdomain.com; path=/';
  document.cookie = '_rwgToken=' + encodeURIComponent(rwgToken) + cookieAttrs;
  if (merchantId !== undefined) {
    document.cookie =
        '_merchantId=' + encodeURIComponent(merchantId) + cookieAttrs;
  }
}
```

### Store at device-level

Device-level storage uses browser cookies, local storage, app local storage, or
any other method that can persist the `rwg_token` and `merchant_id` for the
30-day attribution window. Because the values are stored locally on the user's
device, the conversion event can't attribute properly if the user:

- Changes the device they use.
- Clears the local storage or cookies.
- Uses a private or incognito browser.

When you use device-level conversion tracking, you need to implement conversion
tracking across every supported device, including mobile.

### Store at user-level

User-level storage persists the `rwg_token` and `merchant_id` in your
application database, through a server-side analytics system, or in another
server-side system. Because the values are stored server-side, the conversion
event still attributes properly after the user logs back in.

When you use user-level conversion tracking based on your system architecture,
you can implement the conversion event once on your server-side and reuse it
across all supported devices.

> [!NOTE]
> **Note:** You can decide where to store the `rwg_token` and `merchant_id` based on your requirements.

### Refresh the stored values

When Google refers a user again through an `action_link`, replace both the
stored `rwg_token` and the stored `merchant_id` with the values from the latest
referral. After replacement, the 30-day attribution window resets and any new
conversions attribute to the latest referral.

Refer to [Requirements for Conversion Attribution](https://developers.google.com/actions-center/verticals/local-services/redirect/integration-steps/conversion-tracking.md.txt#attribution-requirements)
for more details.

## Return the `rwg_token` and `merchant_changed` values

When a user completes a booking that begins from an `action_link`
referral, you need to send an HTTP POST request to the conversion endpoint.
There are two endpoints:

- **Production environment**: https://www.google.com/maps/conversion/collect
- **Sandbox environment**: https://www.google.com/maps/conversion/debug/collect

When you send a conversion event, you **must** include the stored `rwg_token`
and a `merchant_changed` integer value of `1` or `2`. For more details on
`merchant_changed`, see [Return the merchant change value](https://developers.google.com/actions-center/verticals/local-services/redirect/integration-steps/conversion-tracking.md.txt#return-merchant).

The POST body must be a JSON-encoded object in the format:

```json
{
  "conversion_partner_id": "<partnerId>",
  "rwg_token": "<rwg_token_val>",
  "merchant_changed": 1|2
}
```

Example payload:

```json
{
  "conversion_partner_id": "XXXXXXX",
  "rwg_token": "AFd1xnHrJWKcjtriCyB5j3QL0bzbmLdcg1N1f5cJuSNVhteYjuOJz18Au6GIAT0tjHkw6fkUJKcarafV45b3c_gl7uT_o8HMcg==",
  "merchant_changed": 2
}
```

The following example shows device-level conversion tracking reading the stored
cookies on the user's device and sending the conversion event in JavaScript:

```javascript
function getCookie(name) {
  const match = document.cookie.match(
      new RegExp('(?:^|; )' + name + '=([^;]+)'));
  return match ? decodeURIComponent(match[1]) : undefined;
}

function getCurrentMerchantId() {
  // TODO: Implement this function to return the unique identifier (string) of
  // the merchant where the user completed a booking, using the
  // same identifier format as getLandingMerchantId().
}

const partnerId = XXXXXXXXXX;
const endpoint = 'https://www.google.com/maps/conversion/collect';

// Retrieve the rwgToken and merchantId stored in the browser's cookies.
const storedRwgToken = getCookie('_rwgToken');
const storedMerchantId = getCookie('_merchantId');

// Compare the stored referral merchant ID against the merchant ID where the
// user completed a booking. If users cannot switch merchants on
// your site, hardcode merchantChanged to 2.
const currentMerchantId = getCurrentMerchantId();
const merchantChanged =
    storedMerchantId === String(currentMerchantId) ? 2 : 1;

// Send the conversion event with the URL-decoded token, ensuring special
// characters such as '=' are included.
if (storedRwgToken !== undefined) {
  fetch(endpoint, {
    method: 'POST',
    body: JSON.stringify({
      conversion_partner_id: partnerId,
      rwg_token: storedRwgToken,
      merchant_changed: merchantChanged
    })
  });
}
```

> [!CAUTION]
> **Caution:** The only accepted `MIME Content-Type` is `text/plain`. **Don't** set any other `Content-Type` in the header, which results in a CORS error.

> [!WARNING]
> **Warning:** Verify that as part of conversion data user Personal Identification Information (PII) is not sent to Google.

> [!NOTE]
> **Note:** In production, you must not send conversion events that aren't attributed to this integration. It's best practice to check for a valid and unexpired `rwg_token` value before you send the event.

### Return the merchant change value

The `merchant_changed` value is used to determine if the merchant changed from
the initial redirect merchant. Change of merchants is common if your landing
page is within a platform that includes other merchants. In this case, if a user
is referred to your platform by Google and decides to navigate to a different
merchant to complete a booking, Google needs to know that the
conversion happened with a separate merchant. Use the `merchant_changed` integer
value (`1` or `2`) to indicate whether the merchant changed, without sending
merchant details.

When deciding which value to assign to `merchant_changed`, compare the original
merchant identifier stored in
[Store the `rwg_token` and `merchant_id`](https://developers.google.com/actions-center/verticals/local-services/redirect/integration-steps/conversion-tracking.md.txt#store-token) against the merchant
that completed a booking:

- **Requirement** : when a user leaves the original merchant's website and completes a purchase through your platform with a different merchant.
  - **Merchant change value** : `1`
- **Requirement** : when the user completes a transaction through the original merchant.
  - **Merchant change value** : `2`

> [!NOTE]
> **Note:** If your booking page isn't within a platform or users are unable to navigate to a different merchant, you can hardcode the value of these requests to `2`. This is common for partners who integrate their booking pages into their merchant's website or have custom landing pages to process a booking.

## Test and Verify the Conversion Tracking

The following test cases use test tokens provided in the
[Test Tokens](https://developers.google.com/actions-center/verticals/local-services/redirect/integration-steps/conversion-tracking.md.txt#test-tokens) section and are intended to help guide you through
all of various scenarios that can result in a conversion event. This ensure that
the token is stored appropriately, the `merchant_changed` value is set
correctly, and the conversion event is sent when appropriate.

Use the action link or booking page URLs provided in your feeds, and run
each test case by appending the
[Test Tokens](https://developers.google.com/actions-center/verticals/local-services/redirect/integration-steps/conversion-tracking.md.txt#test-tokens) to the end of the URL. Be sure to use a private or
incognito browser window -- this will clear any existing tokens associated with
the current user and give you a clean slate to work with.

| **Test Case** | **Test Description** | **User Flow** | **Expected Outcome** |
|---|---|---|---|
| 1 | A user completes a booking that did not originate on Google. | A user navigates directly to the booking page without being referred to by Google or without an existing referral. This shouldn't result in any conversion event. | **No conversion event** because the user did not previously visit the booking page or was not referred by Google. |
| 2 | A user completes a booking that originated on Google. | A user finds your merchant through Google, is referred to your booking page and completes a booking. | **A conversion event would be sent** with *Token A* and *merchant changed* value of *2* because the user was referred to the booking page by Google. |
| 3 | A user (originating from Google) begins the booking flow, but abandons the session before a booking is completed. **Note: Keep this session open for Test 4 and 5.** | A user is referred to your booking page but the session ends and they don't complete a booking. | **No conversion** because the user did not complete a booking but *Token B* should be stored for 30 days. |
| 4 | A user returns to the booking page, without having been originating from Google, and completes a booking. **Note: The booking flow URL must not include an rwg_token.** | A user returns to the booking page after **Test #4** . *Token B* should be stored for 30 days and any conversion during those 30 days should return a conversion event. | **A conversion event would be sent** with *Token B* and *merchant changed* value of *2* because the user is returning to the booking page after previous referral from Google. |
| 5 | A user completes a new booking originating from Google after **Test #4**. | If a user returns to your booking page using a Google referral after a previous Google referral, their 30-day storage window resets and a new token *Token C* replaces the old one *Token B* . All future conversions will then be attributed to *Token C*. | **A conversion event would be sent** with *Token C* and *merchant changed* value of *2* because the user completed the booking and the new token replaced the previously stored token. |

**If you have a platform with the ability for users to checkout with a different merchant, test the following.**

> [!IMPORTANT]
> **Important:** As noted in the [Return the Merchant Change Value](https://developers.google.com/actions-center/verticals/local-services/redirect/integration-steps/conversion-tracking.md.txt#return-merchant) section, you can hardcode the value of these requests to `2` if your booking pages or Action Links don't have the capability for a user to switch merchants. This is common for partners who integrate their booking pages into their merchant's website or have custom landing pages to process a booking. If you're one of these partners, you can skip the following test cases.

| **Test Case** | **Test Description** | **User Flow** | **Expected Outcome** |
|---|---|---|---|
| 6 | A user is referred to your booking page by Google and completes a booking with a different merchant. | A user is referred to your booking page by Google, *Token A* is used, but before completing a booking they navigate to a different page and complete the booking with a merchant that differs from the original referral. | **A conversion event would be sent** because the user completed a booking that originated with a referral from Google with *Token A* and *merchant changed* value of *1* because the user completed the booking in a different merchant than the referral. |

While you test, send the HTTP POST request to the conversion endpoint.
There are two endpoints:

- Production environment: https://www.google.com/maps/conversion/collect
- Sandbox environment: https://www.google.com/maps/conversion/debug/collect

### Test Tokens

To test conversion tracking, add one of the following test tokens to the end of
the Action links or booking page URLs you provide in your feeds.

**Token A:**

    rwg_token=AFd1xnHrJWKcjtriCyB5j3QL0bzbmLdcg1N1f5cJuSNVhteYjuOJz18Au6GIAT0tjHkw6fkUJKcarafV45b3c_gl7uT_o8HMcg%3D%3D

**Token B:**

    rwg_token=AE37R_gte8WCEaytMalsIr9agWQRLuN8199RsVhU2WoRKRvcZ0eBTD8cFvnfUZh-GY0IvY-zjS3W9KWAmy3WDiWXbVvQRZyzMQ%3D%3D

**Token C:**

    rwg_token=AE37R_jmAP-UgWNApkxwdlX5M2z1UBGuUzHZ63YWmRWeTjRJKxfSD1mzJ4joLnB11MHrBULuisjP-9I64LnBJDXyCc_CIJSCPg%3D%3D

When you send the successful conversion events, you can see them aggregated
in the Action Center under the conversion tracking dashboard.

> [!NOTE]
> **Note:** Conversions can take up to an hour to appear in the dashboard.

![conversion-tracking-dashboard](https://developers.google.com/static/actions-center/images/conversion-tracking-dashboard.png)

### Requirements for Conversion Attribution

Google's required standard for conversion attribution is a 30 days attribution
window for any interaction with a place action link, at any store.

This attribution window means that Google can expect a conversion event to be
sent in any of the following scenarios:

- A user follows a place action link and places an order for the same merchant in the same session. Merchant change value = 2.
- A user follows a place action link and then returns from a different channel within the 30 days attribution window to place an order for the same merchant. Merchant change value = 2.
- A user follows a place action link and then places an order at a different store, either within the same session or a different session within a 30 days attribution window. Merchant change value = 1.

Additionally, Google expects to send conversion events from any device, a user
can access through a place action link. These devices include:

- Desktop or mobile web applications.
- Mobile apps, either through an app deep link or a registered app-intent for your domain.

If the token is stored at the user-level, it's expected that you provide
cross-device attribution. For more information, see
[Store at User-Level](https://developers.google.com/actions-center/verticals/local-services/redirect/integration-steps/conversion-tracking.md.txt#user-level). In this case, a user who follows an action
link from the desktop and then completes the transaction on mobile with the same
user account, must trigger a conversion event.

If the token is stored exclusively at the device-level, such as in browser
cookies, it's not expected that you provide cross-device attribution. In this
case, each device can have a separate token persisted, if the user follows an
action link on that device, and each device can follow the attribution
rules separately.

> [!NOTE]
> **Note:** This attribution framework can be different from your analytics system attributes conversions. This system is designed to operate independently of any conversion tracking you do in your systems, and need not affect your internal analytics.