# Getting cookie information
> Documentation for Getting cookie information in the web tracker.
> Source: https://docs.snowplow.io/docs/sources/web-trackers/previous-versions/web-trackers-v3/cookies-and-local-storage/getting-cookie-values/
> Documentation index: https://docs.snowplow.io/llms.txt
> Status: Previous version
> Current version: https://docs.snowplow.io/docs/sources/web-trackers/

## Domain user information

You can use the following function to extract the Domain User Information from the ID cookie:

```javascript
/*
* Function to extract the Snowplow Domain User Information from the first-party cookie set by the Snowplow JavaScript Tracker
*
* @param string cookieName (optional) The value used for "cookieName" in the tracker constructor argmap
* (leave blank if you did not set a custom cookie name)
*
* @return string or bool The ID string if the cookie exists or false if the cookie has not been set yet
*/
function getSnowplowDuid(cookieName) {
  var cookieName = cookieName || '_sp_';
  var matcher = new RegExp(cookieName + 'id\\.[a-f0-9]+=([^;]+);?');
  var match = document.cookie.match(matcher);
  var split = match[1].split('.');
  if (match && match[1]) {
    return {
      'domain_userid': split[0],
      'domain_sessionidx': split[2],
      'domain_sessionid': split[5]
    }
  } else {
    return false;
  }
}
```

If you set a custom `cookieName` field in the argmap, pass that name into the function; otherwise call the function without arguments. Note that if the function is called before the cookie exists (i.e. when the user is visiting the page for the first time and sp.js has not yet loaded) if will return `false`.

## Retrieving cookie properties from the tracker

It's possible to retrieve cookie properties for use in your code (as well as the [page view UUID](/docs/sources/web-trackers/previous-versions/web-trackers-v3/tracking-events/page-views/) and [user ID](/docs/sources/web-trackers/previous-versions/web-trackers-v3/tracking-events/#getting-user-id-once-set)) using a tracker callback. This is an advanced usage of the tracker.

**JavaScript (tag):**

If you call `snowplow` with a function as the argument, the function will be executed when `sp.js` loads:

```javascript
snowplow(function () {
  console.log("sp.js has loaded");
});
```

Or equivalently:

```javascript
snowplow(function (x) {
  console.log(x);
}, "sp.js has loaded");
```

The callback you provide is executed as a method on the internal `trackerDictionary` object. You can access the `trackerDictionary` using `this`.

```javascript
// Configure a tracker instance named "sp"
snowplow('newTracker', 'sp', '{{COLLECTOR_URL}', {
 appId: 'snowplowExampleApp'
});

// Access the tracker instance inside a callback
snowplow(function () {
 var sp = this.sp;
 var domainUserId = sp.getDomainUserId();
 console.log(domainUserId);
})
```

The callback function shouldn't be a method:

```javascript
// TypeError: Illegal invocation
snowplow(console.log, "sp.js has loaded");
```

This won't work because the value of `this` in the `console.log` function will be the `trackerDictionary`, rather than `console`.

You can get around this problem using `Function.prototoype.bind` as follows:

```javascript
snowplow(console.log.bind(console), "sp.js has loaded");
```

For more on execution context in JavaScript, see the [MDN page](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this).

**Browser (npm):**

When initialising a tracker, you can use the returned `tracker` instance to access various properties from this tracker instance.

```javascript
// Configure a tracker instance named "sp"
const sp = newTracker('sp', '{{COLLECTOR_URL}', {
 appId: 'snowplowExampleApp'
});

// Access the tracker properties
const domainUserId = sp.getDomainUserId();
```

***

### `getDomainUserId`

The `getDomainUserId` method returns the user ID stored in the first-party cookie:

**JavaScript (tag):**

```javascript
// Access the tracker instance inside a callback
snowplow(function () {
 var sp = this.sp;
 var domainUserId = sp.getDomainUserId();
 console.log(domainUserId);
})
```

**Browser (npm):**

```javascript
const domainUserId = sp.getDomainUserId();
console.log(domainUserId);
```

***

### `getDomainUserInfo`

The `getDomainUserInfo` method returns all the information stored in first-party cookie in an array:

**JavaScript (tag):**

```javascript
// Access the tracker instance inside a callback
snowplow(function () {
 var sp = this.sp;
 var domainUserInfo = sp.getDomainUserInfo();
 console.log(domainUserInfo);
})
```

**Browser (npm):**

```javascript
const domainUserInfo = sp.getDomainUserInfo();
console.log(domainUserInfo);
```

***

The `domainUserInfo` variable will contain an array with 11 elements:

1. A string set to `'1'` if this is the user's first session and `'0'` otherwise
2. The domain user ID
3. The timestamp at which the cookie was created
4. The number of times the user has visited the site
5. The timestamp for the current visit
6. The timestamp of the last visit
7. The session id
8. ID of the previous session (since version 3.5)
9. ID of the first event in the current session (since version 3.5)
10. Device created timestamp of the first event in the current session (since version 3.5)
11. Index of the last event in the session (used to inspect order of events) (since version 3.5)

### `getCookieName`

The `getCookieName` method returns the complete cookie name for the domain or session cookie:

**JavaScript (tag):**

```javascript
// Access the tracker instance inside a callback
snowplow(function () {
 var sp = this.sp;
 var cookieName = sp.getCookieName('id');
 console.log(cookieName);
})
```

**Browser (npm):**

```javascript
const cookieName = sp.getCookieName('id');
console.log(cookieName);
```

***

The argument corresponds to the basename of the cookie: 'id' for the domain cookie, 'ses' for the session cookie.
