---
title: "Segment · Nuxt Scripts"
description: "Load Segment Analytics.js and queue page, event, identity, and group calls."
canonical_url: "https://scripts.nuxt.com/scripts/segment"
meta:
  "og:description": "Load Segment Analytics.js and queue page, event, identity, and group calls."
  "og:image": "https://scripts.nuxt.com/_og/s/o_334e5384f959d143.png"
  "og:image:height": "600"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "Segment · Nuxt Scripts"
  "twitter:image": "https://scripts.nuxt.com/_og/s/o_334e5384f959d143.png"
---

> Index of every page on this site: [llms.txt](https://scripts.nuxt.com/llms.txt)

**Scripts**

# **Segment**

[Copy for LLMs](https://scripts.nuxt.com/scripts/segment.md)

[**Segment**](https://www.twilio.com/en-us/segment) routes events from your site to analytics, marketing, and data warehouse destinations.

Use [`**useScriptSegment()**`](https://scripts.nuxt.com/scripts/segment) to load Analytics.js and access its tracking methods. Segment's [**Analytics.js quickstart**](https://www.twilio.com/docs/segment/connections/sources/catalog/libraries/website/javascript/quickstart) explains write keys and the `**page**`, `**track**`, and `**identify**` calls exposed by the library.

The client initializer queues one `**page()**` call before Analytics.js loads. Avoid sending another page call for the initial route unless you intend to count it twice.

**Segment**

[**View source **](https://github.com/nuxt/scripts/blob/main/packages/script/src/runtime/registry/segment.ts)

## Nuxt Config Setup

Add this to your `**nuxt.config.ts**` to load Segment globally. Alternatively you can use the **useScriptSegment** composable for more control.

```
export default defineNuxtConfig({
  scripts: {
    registry: {
      segment: {
        writeKey: 'YOUR_WRITE_KEY',
        trigger: 'onNuxtReady',
      }
    }
  }
})
```

This config automatically enables **first-party mode ** (bundle). See below to customise.

## useScriptSegment()

The `**useScriptSegment**` composable lets you have fine-grain control over when and how Segment is loaded on your site.

```
const { proxy } = useScriptSegment()

proxy.track('conversion', { value: 1, currency: 'USD' })
```

Please follow the [**Registry Scripts **](https://scripts.nuxt.com/docs/guides/registry-scripts) guide to learn more about advanced usage.

## First-Party Mode: Privacy Focused Proxy

No extra config needed. The script is bundled and served from your domain instead of a third-party CDN, eliminating an extra DNS lookup and improving load times. [**Learn more. **](https://scripts.nuxt.com/docs/guides/first-party)

**Mode**

Bundle Partytown

```
export default defineNuxtConfig({
  scripts: {
    // ✅ First-party mode: bundled
    registry: {
      segment: {
        writeKey: 'YOUR_WRITE_KEY',
        trigger: 'onNuxtReady',
      },
    },
  },
})
```

## Example

Using Segment in a component.

ConversionButton.vue

```
<script setup lang="ts">
const { proxy } = useScriptSegment()

// noop in development, ssr
// just works in production, client
function handleAction() {
  proxy.track('conversion', { value: 1, currency: 'USD' })
}
</script>

<template>
  <div>
    <button @click="handleAction">
      Send Event
    </button>
  </div>
</template>
```

`**writeKey**``string`** required **

Your Segment write key.

`**analyticsKey**``string` = 'analytics'

The global variable name for the analytics instance.

[Edit this page](https://github.com/nuxt/scripts/edit/main/docs/content/segment.md)

[Markdown For LLMs](https://scripts.nuxt.com/scripts/segment.md)

[**Rybbit Analytics** Load hosted or self-hosted Rybbit Analytics and call its tracking API.](https://scripts.nuxt.com/scripts/rybbit-analytics) [**Snapchat Pixel** Load Snapchat Pixel and opt in to automatic or manual page-view events.](https://scripts.nuxt.com/scripts/snapchat-pixel)

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://scripts.nuxt.com/#website","@type":"WebSite","description":"Nuxt Scripts lets you load third-party scripts with better performance, privacy, security and DX. It includes many popular third-parties out of the box.","name":"Nuxt Scripts","url":"https://scripts.nuxt.com/","publisher":{"@id":"https://scripts.nuxt.com/#identity"}},{"@id":"https://scripts.nuxt.com/scripts/segment#webpage","@type":"WebPage","description":"Load Segment Analytics.js and queue page, event, identity, and group calls.","name":"Segment","url":"https://scripts.nuxt.com/scripts/segment","breadcrumb":{"@id":"https://scripts.nuxt.com/scripts/segment#breadcrumb"},"isPartOf":{"@id":"https://scripts.nuxt.com/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://scripts.nuxt.com/scripts/segment"]}],"primaryImageOfPage":{"@id":"https://scripts.nuxt.com/#logo"}},{"@id":"https://scripts.nuxt.com/#identity","@type":"Organization","name":"Nuxt Scripts","url":"https://scripts.nuxt.com","logo":{"@id":"https://scripts.nuxt.com/#logo"}},{"@id":"https://scripts.nuxt.com/scripts/segment#breadcrumb","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","name":"Nuxt Scripts","item":"https://scripts.nuxt.com/","position":1},{"@type":"ListItem","name":"Scripts","item":"https://scripts.nuxt.com/scripts","position":2},{"@type":"ListItem","name":"Segment","item":"https://scripts.nuxt.com/scripts/segment","position":3}]},{"@id":"https://scripts.nuxt.com/#logo","@type":"ImageObject","caption":"Nuxt Scripts","contentUrl":"https://scripts.nuxt.com/logo.svg","inLanguage":"en","url":"https://scripts.nuxt.com/logo.svg"}]}
```

## Sitemap

See the full [sitemap](https://scripts.nuxt.com/sitemap.md) for all pages.
