---
title: "useScriptTriggerInteraction() · Nuxt Scripts"
description: "Load a script after a configured user interaction."
canonical_url: "https://scripts.nuxt.com/docs/api/use-script-trigger-interaction"
meta:
  "og:description": "Load a script after a configured user interaction."
  "og:image": "https://scripts.nuxt.com/_og/s/o_d306e28a96be1539.png"
  "og:image:height": "600"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "useScriptTriggerInteraction() · Nuxt Scripts"
  "twitter:image": "https://scripts.nuxt.com/_og/s/o_d306e28a96be1539.png"
---

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

**Api**

# **useScriptTriggerInteraction()**

[Copy for LLMs](https://scripts.nuxt.com/docs/api/use-script-trigger-interaction.md)

Load a script when any configured interaction event occurs.

Listeners are attached inside `**onNuxtReady**`, so interactions that happen before Nuxt is ready do not count. Passing an empty `**events**` array throws an error.

## Signature

```ts
function useScriptTriggerInteraction(options: InteractionScriptTriggerOptions): UseScriptTrigger
```

## Arguments

```ts
export interface InteractionScriptTriggerOptions {
  /**
   * The interaction events to listen for.
   */
  events: string[]
  /**
   * The element to listen for events on.
   * @default document.documentElement
   */
  target?: EventTarget | null
}
```

## Returns

An Unhead trigger function for `**scriptOptions.trigger**`. It loads the script after the first matching event, then removes every listener. Disposing the consumer scope removes pending listeners, including when disposal happens before Nuxt becomes ready. A `**null**` target leaves the script unloaded.

## Nuxt Config Usage

Registry entries and global scripts accept the trigger directly in `**nuxt.config**`:

```ts
export default defineNuxtConfig({
  scripts: {
    registry: {
      googleAnalytics: {
        id: 'GA_MEASUREMENT_ID',
        trigger: { interaction: ['scroll', 'click', 'keydown'] }
      }
    }
  }
})
```

## Examples

### Basic Usage

Load a script when the user scrolls, clicks, or presses a key:

```ts
const script = useScript({
  src: 'https://example.com/chat-widget.js',
}, {
  trigger: useScriptTriggerInteraction({
    events: ['scroll', 'click', 'keydown']
  })
})
```

### Analytics on First Interaction

Load analytics only when users interact with your site:

```vue
<script setup lang="ts">
// Load analytics on any user interaction
const { proxy, status } = useScriptGoogleAnalytics({
  id: 'GA_MEASUREMENT_ID',
  scriptOptions: {
    trigger: useScriptTriggerInteraction({
      events: ['click', 'scroll', 'keydown', 'touchstart']
    })
  }
})

// Track the first interaction
watch(status, (value) => {
  if (value === 'loaded') {
    proxy.gtag('event', 'first_interaction', {
      event_category: 'engagement'
    })
  }
})
</script>
```

### Specific Element Targeting

The `**target**` option accepts an `**EventTarget**` that already exists when the composable runs. For Vue template refs, use [`**useScriptTriggerElement()**`](https://scripts.nuxt.com/docs/api/use-script-trigger-element), which accepts a computed element ref. To preserve events that occur before hydration, bind the returned `**ssrAttrs**` to the rendered element as shown in its API reference.

## Choosing Events

- Listen for interactions that indicate users will need the script soon.
- Include keyboard and touch events when the feature supports those input methods.
- Use `**target**` to limit listeners to the relevant part of the page.
- If the script must load by a deadline even without interaction, combine the event listeners and timeout in one custom trigger function.

[Edit this page](https://github.com/nuxt/scripts/edit/main/docs/content/3.api/3.use-script-trigger-interaction.md)

[Markdown For LLMs](https://scripts.nuxt.com/docs/api/use-script-trigger-interaction.md)

[**useScriptTriggerIdleTimeout()** Load a script after Nuxt is ready and an idle timeout has elapsed.](https://scripts.nuxt.com/docs/api/use-script-trigger-idle-timeout) [**useScriptEventPage()** React to changes in the current page title or path.](https://scripts.nuxt.com/docs/api/use-script-event-page)

**On this page **

- [Signature](#signature)
- [Arguments](#arguments)
- [Returns](#returns)
- [Nuxt Config Usage](#nuxt-config-usage)
- [Examples](#examples)
- [Choosing Events](#choosing-events)

```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/docs/api/use-script-trigger-interaction#webpage","@type":"WebPage","description":"Load a script after a configured user interaction.","name":"useScriptTriggerInteraction()","url":"https://scripts.nuxt.com/docs/api/use-script-trigger-interaction","breadcrumb":{"@id":"https://scripts.nuxt.com/docs/api/use-script-trigger-interaction#breadcrumb"},"isPartOf":{"@id":"https://scripts.nuxt.com/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://scripts.nuxt.com/docs/api/use-script-trigger-interaction"]}],"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/docs/api/use-script-trigger-interaction#breadcrumb","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","name":"Nuxt Scripts","item":"https://scripts.nuxt.com/","position":1},{"@type":"ListItem","name":"Documentation","item":"https://scripts.nuxt.com/docs","position":2},{"@type":"ListItem","name":"API","item":"https://scripts.nuxt.com/docs/api","position":3},{"@type":"ListItem","name":"useScriptTriggerInteraction()","item":"https://scripts.nuxt.com/docs/api/use-script-trigger-interaction","position":4}]},{"@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.
