# Supporting Google Analytics conversion tracking

> [!NOTE]
> **Objective:** Implement conversion tracking for Actions Center Legacy Integration using Google Analytics.

> [!NOTE]
> **Note:** Please ensure that as part of conversion data, user Personal Identification Information (PII) should **not** be sent to Google without expressed user consent or as otherwise permitted under law.

> [!NOTE]
> **Note:** Partners do **not** need to set up their own Google Analytics account in order to participate in conversion tracking.

### Instructions

There are 3 phases for implementing conversion tracking:

1. Setting up Google Analytics(GA) site tag
2. Persisting Actions Center URL parameter
3. Sending Conversion Data

### Google Analytics site Tag setup.

Work with you Google contact to generate a `Measurement ID`
for your Actions Center account.

> [!IMPORTANT]
> **Key Point:** Store this Measurement ID , as this will be required to implement conversion tracking for your account.

Add the following code snippet after the `head` tags on all web pages
that:

- a conversion can occur
- the landing page for all `action_links` that are Reserve with Google enabled.

#### No Existing Google Analytics Integration

If this is the first time creating a Google Analytics site(gtag.js), use the
following code snippet:

```javascript
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID_PROVIDED_BY_GOOGLE"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config',
       'GA_MEASUREMENT_ID_PROVIDED_BY_GOOGLE', // Replace with Measurement ID
       {
         // DO NOT EDIT THE BELOW MENTIONED PARAMETERS
         // AS OVERRIDING RESULTS IN SENDING US PII INFORMATION.
         'referrer' : undefined,
         'page_title' : undefined,
         'page_path': undefined,
         'page_location': undefined,
         'send_page_view': false,
         'anonymize_ip': true,
         'groups': 'reserve_with_google'
       });
</script>
```

#### Existing Google Analytics Integration

If you already have a Google Analytics site tag(gtag.js) for an existing
integration, use the following code snippet:

```javascript
gtag('config',
       'GA_MEASUREMENT_ID_PROVIDED_BY_GOOGLE',  //Replace with Measurement ID
       {
         'referrer' : undefined,
         'page_title' : undefined,
         'page_path': undefined,
         'page_location': undefined,
         'send_page_view': false,
         'anonymize_ip': true,
         'groups': 'reserve_with_google'
       });
```

Update the above mentioned code snippets with the generated Measurement ID
for your account.

### Persisting Actions Center URL Parameter

To properly track conversions from `action link`(s), Google will
set a URL parameter `rwg_token`, which should be returned at the
time of a conversion.

You will be required to persist the `rwg_token` URL parameter
which will be appended to all action links provided by you for a maximum
duration of 30 days when a user visits the landing page via Google. The
preferred way to persist this information is via cookies.

> [!NOTE]
> **Note:** The value of the `rwg_token` should be stored and returned without any edits.

1. Add the following script on the landing page for all your `action_link`(s) that are Actions Center enabled.
2. Update the rootdomain with your domain.

```javascript
<script>
  var query = location.search.substring(1);
  var params = query.split('&');
  var rwg_token = undefined;
  for (var i = 0; i < params.length; ++i) {
    var pair = params[i].split('=');
    if (pair[0] == 'rwg_token') {
      rwg_token = decodeURIComponent(pair[1]);
      break;
    }
  }
  if (typeof rwg_token == 'undefined') {
    document.cookie =
         "_rwg_token=" + rwg_token + ";max-age=2592000;domain=rootdomain.com;path=/";
  }
</script>
```

### Sending Conversion Data

When a user completes a transaction which originated from a Google Place
Action link, you need to trigger the code mentioned below in order to
complete conversion tracking.

> [!NOTE]
> **Note:** When triggering this event, you will need to provide the `rwg_token` value described in the previous step.

```javascript
<script>
  function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i <ca.length; i++) {
      var c = ca[i];
      while (c.charAt(0) == ' ') {
        c = c.substring(1);
      }
      if (c.indexOf(name) == 0) {
        return c.substring(name.length, c.length);
      }
    }
    return "";
  }

  if (leadFromReserveWithGoogle()) { // implement a function to identify the conversion is originating via Google
    gtag('event', 'rwg_conversion', {
        'rwg_token': getCookie('_rwg_token'),
        'send_to': 'reserve_with_google'
    });
  }
</script>
```