> For the complete Lynkow documentation index in agent-friendly format, see [llms.txt](/llms.txt).

# AnalyticsService

**Published**: 2026-11-30
**Category**: Services

Loads the Lynkow analytics tracker in the browser and sends pageviews and
events through it. Available as `lynkow.analytics`; every method is a
no-op on the server. In a browser, `createClient()` calls `init()` for
you, and the tracker records pageviews itself, client-side route changes
included. It follows the site's cookie consent mode: on an `'opt-in'` site
it loads only after the visitor accepts analytics.

Access via: `lynkow.analytics`

## Methods

**9** methods

### `destroy`

```typescript
destroy(): void
```

Removes the tracker script element, cancels a load still waiting for
consent and resets this service, so `init()` can run again. A tracker
that already ran keeps recording in the page until it reloads. No-op on
the server.

Returns: `void`

```typescript
// Clean up analytics when unmounting (e.g. React useEffect cleanup)
useEffect(() => {
  lynkow.analytics.init()
  return () => lynkow.analytics.destroy()
}, [])
```

---

### `disable`

```typescript
disable(): void
```

Disables analytics tracking. While disabled, all `trackEvent()` and
`trackPageview()` calls become no-ops. The tracker script remains loaded;
call `destroy()` to fully remove it.

Returns: `void`

```typescript
// Disable tracking when the user revokes analytics consent
lynkow.analytics.disable()
```

---

### `enable`

```typescript
enable(): void
```

Enables analytics tracking. Tracking is enabled by default when the
service is created. Call this to re-enable after a previous `disable()` call.

Returns: `void`

```typescript
// Re-enable tracking after the user grants analytics consent
lynkow.on('consent-changed', (categories) => {
  if (categories.analytics) {
    lynkow.analytics.enable()
  } else {
    lynkow.analytics.disable()
  }
})
```

---

### `getTracker`

```typescript
getTracker(): LynkowAnalyticsGlobal | undefined
```

Returns the loaded tracker (`window.LynkowAnalytics`), or `undefined` on
the server and before it loads. Its `setConsent(granted)` passes the
visitor's analytics answer when consent is collected by a tool other than
the Lynkow banner.

Returns: `LynkowAnalyticsGlobal | undefined`

```typescript
lynkow.analytics.getTracker()?.setConsent?.(true)
```

---

### `init`

```typescript
init(): Promise<void>
```

Loads the tracker script from `{baseUrl}/analytics/tracker.js` once the
site's cookie consent configuration has answered, and passes it the
consent mode. `createClient()` already calls it in a browser, and once
the tracker is loaded later calls do nothing. On an `'opt-in'` site the script loads only after the visitor
accepts analytics (now, earlier or in another tab). If the configuration
cannot be read, `'opt-in'` is assumed. An existing `#lynkow-tracker`
script is reused. No-op on the server. Never rejects: a load failure is
logged to the console and retried on the next call.

Returns: `Promise<void>`

```typescript
await lynkow.analytics.init()
```

---

### `isEnabled`

```typescript
isEnabled(): boolean
```

Returns whether analytics tracking is currently enabled (i.e. whether
`trackEvent` and `trackPageview` will actually emit). Does not
indicate whether the underlying tracker script has loaded; use
isInitialized for that.

Returns: `boolean`

```typescript
if (lynkow.analytics.isEnabled()) {
  // Safe to track
}
```

---

### `isInitialized`

```typescript
isInitialized(): boolean
```

Returns whether the tracker.js script has been loaded and the
`window.LynkowAnalytics` global is available. Always `false` on the
server.

Returns: `boolean`

```typescript
if (lynkow.analytics.isInitialized()) {
  lynkow.analytics.trackEvent({
    type: 'click',
    click_element_text: 'Start trial',
  })
}
```

---

### `trackEvent`

```typescript
trackEvent(event: EventData): Promise<void>
```

Sends an analytics event through the tracker, calling init first.
The event is posted to `/storefront/{siteId}/analytics/events` with
`page_url` and `page_path` taken from the current location unless the
payload sets them. Nothing is sent on the server, after disable,
or while the visitor's consent does not allow analytics. The API answers
every event the same way, so an invalid one is dropped without error.

| Parameter | Type | Description |
| --- | --- | --- |
| `event` | `EventData` | `type` plus extra fields in the API's snake_case spelling, see EventData |


Returns: `Promise<void>`

```typescript
await lynkow.analytics.trackEvent({
  type: 'outbound_link',
  click_target_url: 'https://partner.example.com/pricing',
  click_element_text: 'See pricing',
})
```

---

### `trackPageview`

```typescript
trackPageview(data?: PageviewData): Promise<void>
```

Records a pageview through the tracker, calling init first. The
tracker already records the first load and every History API or hash
navigation, so call this only for a view it cannot see, such as a modal
or tab that changes no URL. Calling it on a route change records the page
twice. Nothing is sent on the server, after disable, or while the
visitor's consent does not allow analytics.

| Parameter | Type | Description |
| --- | --- | --- |
| `data` | `PageviewData` | Page path, title, referrer and URL to record; each defaults to the current browser value |


Returns: `Promise<void>`

```typescript
await lynkow.analytics.trackPageview({ path: '/pricing/compare', title: 'Compare plans' })
```