Scripts

Segment lets you collect, clean, and control your customer data. Segment helps you to understand your customers and personalize their experience.

Nuxt Scripts provides a registry script composable useScriptSegment() to easily integrate Segment in your Nuxt app.

Segment

View source

Nuxt Config Setup

The simplest way to load Segment globally in your Nuxt App is to use Nuxt config. Alternatively you can directly use the useScriptSegment composable.

export default defineNuxtConfig({
  scripts: {
    registry: {
      segment: {
        writeKey: 'YOUR_WRITE_KEY'
      }
    }
  }
})

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 guide to learn more about advanced usage.

First-Party Mode

This script supports First-Party Mode which is auto-enabled, routing all traffic through your domain for improved privacy and ad blocker bypass.

export default defineNuxtConfig({
  scripts: {
    registry: {
      segment: {
        writeKey: 'YOUR_WRITE_KEY'
      }
    }
  }
})

Example

Using Segment only in production while using the proxy to send events.

<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>
writeKeystring required

Your Segment write key.

analyticsKeystring = 'analytics'

The global variable name for the analytics instance.