# Track Google Analytics cookies with the web trackers
> Automatically capture Google Analytics cookie values including GA4 and Universal Analytics cookies as context entities on every event.
> Source: https://docs.snowplow.io/docs/sources/web-trackers/tracking-events/ga-cookies/
> Documentation index: https://docs.snowplow.io/llms.txt

If this plugin is used, the tracker will look for Google Analytics cookies (GA4/Universal Analytics and "classic" GA; specifically the `_ga` cookie and older `__utma`, `__utmb`, `__utmc`, `__utmv`, `__utmz`) and combine their values into event context entities that get sent with every event.

GA cookies information is **automatically tracked** once configured.

## Install plugin

**JavaScript (tag):**

| Tracker Distribution | Included |
| -------------------- | -------- |
| `sp.js`              | ✅        |
| `sp.lite.js`         | ❌        |

**Download:**

|                                             |                                                                                                           |
| ------------------------------------------- | --------------------------------------------------------------------------------------------------------- |
| Download from GitHub Releases (Recommended) | [Github Releases (plugins.umd.zip)](https://github.com/snowplow/snowplow-javascript-tracker/releases)     |
| Available on jsDelivr                       | [jsDelivr](https://cdn.jsdelivr.net/npm/@snowplow/browser-plugin-ga-cookies@4.10.2/dist/index.umd.min.js) |
| Available on unpkg                          | [unpkg](https://unpkg.com/@snowplow/browser-plugin-ga-cookies@4.10.2/dist/index.umd.min.js)               |

**Note:** The links to the CDNs above are pinned to a specific version at the time of writing. Check the [plugin's GitHub releases](https://github.com/snowplow/snowplow-javascript-tracker/releases) for newer versions, and update the pinned version when you upgrade.

**Browser (npm):**

- `npm install @snowplow/browser-plugin-ga-cookies`
- `yarn add @snowplow/browser-plugin-ga-cookies`
- `pnpm add @snowplow/browser-plugin-ga-cookies`

***

## Initialization

**JavaScript (tag):**

```javascript
window.snowplow('addPlugin',
  "https://cdn.jsdelivr.net/npm/@snowplow/browser-plugin-ga-cookies@4.10.2/dist/index.umd.min.js",
  ["snowplowGaCookies", "GaCookiesPlugin"],
  [pluginOptions] // note: for sp.js, pluginOptions can also be specified when calling `newTracker`; e.g. `contexts: { gaCookies: { ua: true, ga4: false } }`
);
```

**Browser (npm):**

```javascript
import { newTracker, trackPageView } from '@snowplow/browser-tracker';
import { GaCookiesPlugin } from '@snowplow/browser-plugin-ga-cookies';

newTracker('sp1', '{{collector_url}}', {
   appId: 'my-app-id',
   plugins: [ GaCookiesPlugin(pluginOptions) ],
});
```

***

The `pluginOptions` parameter allows to configure the plugin. Its type is:

```javascript
interface GACookiesPluginOptions {
  ua?: boolean;
  ga4?: boolean;
  ga4MeasurementId?: string | string[];
  cookiePrefix?: string | string[];
}
```

| Name             | Default | Description                                                                                                                                                                                                                                                                                                                                                     |
| ---------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ua               | `false` | Send Universal Analytics specific cookie values.                                                                                                                                                                                                                                                                                                                |
| ga4              | `true`  | Send Google Analytics 4 specific cookie values.                                                                                                                                                                                                                                                                                                                 |
| ga4MeasurementId | `""`    | Measurement id(s) to search the Google Analytics 4 session cookie. Can be a single measurement id as a string or an array of measurement id strings. The cookie has the form of `<cookie_prefix>_ga_<container-id>` where `<container-id>` is the data stream container id and `<cookie_prefix>` is the optional `cookie_prefix` option of the gtag.js tracker. |
| cookiePrefix     | `[]`    | Cookie prefix set on the Google Analytics 4 cookies using the `cookie_prefix` option of the gtag.js tracker.                                                                                                                                                                                                                                                    |

## Context entities

Adding this plugin will automatically capture the following entities:

1. For GA4 cookies: `iglu:com.google.ga4/cookies/jsonschema/1-0-0` (default)

   ```json
   {
       "_ga": "G-1234",
       "cookie_prefix": "prefix",
       "session_cookies": [
           {
               "measurement_id": "G-1234",
               "session_cookie": "567"
           }
       ]
   }
   ```

2. For Universal Analytics cookies: `iglu:com.google.analytics/cookies/jsonschema/1-0-0` (if enabled)

   ```json
   {
       "_ga": "GA1.2.3.4"
   }
   ```
