---
title: "useScriptTriggerInteraction · Nuxt Scripts"
description: "API documentation for the useScriptTriggerInteraction function."
canonical_url: "https://scripts.nuxt.com/docs/v0/api/use-script-trigger-interaction"
meta:
  "og:description": "API documentation for the useScriptTriggerInteraction function."
  "og:image": "https://scripts.nuxt.com/_og/d/c_Docs%2Ctitle_useScriptTriggerInteraction%2Cdescription_API%2Bdocumentation%2Bfor%2Bthe%2BuseScriptTriggerInteraction%2Bfunction.%2Cheadline_Api%2Cp_Ii9kb2NzL3YwL2FwaS91c2Utc2NyaXB0LXRyaWdnZXItaW50ZXJhY3Rpb24i%2Cs_-j_AXbMYVygFDksk.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/d/c_Docs%2Ctitle_useScriptTriggerInteraction%2Cdescription_API%2Bdocumentation%2Bfor%2Bthe%2BuseScriptTriggerInteraction%2Bfunction.%2Cheadline_Api%2Cp_Ii9kb2NzL3YwL2FwaS91c2Utc2NyaXB0LXRyaWdnZXItaW50ZXJhY3Rpb24i%2Cs_-j_AXbMYVygFDksk.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/v0/api/use-script-trigger-interaction.md)

Create a trigger that loads a script when any of the specified user interaction events occur. This is perfect for loading scripts only when users actually interact with your site, providing excellent performance optimization.

## Signature

```ts
function useScriptTriggerInteraction(options: InteractionScriptTriggerOptions): Promise<boolean>
```

## 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

A promise that resolves to `**true**` when any of the specified interaction events occur and the script should be loaded, or `**false**` if the trigger is cancelled.

## Nuxt Config Usage

For convenience, you can use this trigger directly in your `**nuxt.config**` without calling the composable explicitly:

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

## Common Event Types

Here are some commonly used interaction events:

- **Mouse events**: `**click**`, `**mousedown**`, `**mouseup**`, `**mouseover**`, `**mouseenter**`, `**mouseleave**`
- **Touch events**: `**touchstart**`, `**touchend**`, `**touchmove**`
- **Keyboard events**: `**keydown**`, `**keyup**`, `**keypress**`
- **Scroll events**: `**scroll**`, `**wheel**`
- **Focus events**: `**focus**`, `**blur**`, `**focusin**`, `**focusout**`

## 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 actually interact with your site:

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

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

### Specific Element Targeting

Listen for events on a specific element:

```vue
<script setup lang="ts">
const headerEl = ref<HTMLElement>()

const script = useScript({
  src: 'https://example.com/header-widget.js'
}, {
  trigger: useScriptTriggerInteraction({
    events: ['click', 'mouseover'],
    target: headerEl
  })
})
</script>

<template>
  <header ref="headerEl">
    <!-- Script loads when this header is clicked or hovered -->
    <nav>...</nav>
  </header>
</template>
```

### Progressive Feature Loading

Load features based on different interaction types:

```vue
<script setup lang="ts">
// Load search enhancement on any input interaction
const searchScript = useScript({
  src: 'https://cdn.example.com/search.js'
}, {
  trigger: useScriptTriggerInteraction({
    events: ['click', 'focus', 'keydown']
  })
})

// Load social sharing only on scroll or share button interaction
const socialScript = useScript({
  src: 'https://cdn.example.com/social-sharing.js'
}, {
  trigger: useScriptTriggerInteraction({
    events: ['scroll', 'click']
  })
})
</script>
```

### Mobile-Optimized Loading

Use touch events for mobile-specific optimizations:

```vue
<script setup lang="ts">
// Load mobile-specific features on touch interactions
const mobileScript = useScript({
  src: 'https://cdn.example.com/mobile-features.js'
}, {
  trigger: useScriptTriggerInteraction({
    events: ['touchstart', 'touchmove', 'scroll']
  })
})
</script>
```

## Best Practices

- **Start with common events**: Use `**['scroll', 'click', 'keydown']**` as a good default for most use cases
- **Consider mobile users**: Include touch events like `**touchstart**` for mobile optimization
- **Target specific elements**: Use the `**target**` option to listen only on relevant parts of your page
- **Combine events wisely**: Don't overload with too many event types - focus on meaningful interactions
- **Test performance impact**: Verify that interaction-based loading actually improves your metrics
- **Fallback considerations**: Consider what happens if users don't interact (e.g., combine with a timeout trigger)

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

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

[**useScriptTriggerIdleTimeout** API documentation for the useScriptTriggerIdleTimeout function.](https://scripts.nuxt.com/docs/v0/api/use-script-trigger-idle-timeout) [**useScriptEventPage** API documentation for the useScriptEventPage function.](https://scripts.nuxt.com/docs/v0/api/use-script-event-page)

**On this page **

- [Signature](#signature)
- [Arguments](#arguments)
- [Returns](#returns)
- [Nuxt Config Usage](#nuxt-config-usage)
- [Common Event Types](#common-event-types)
- [Examples](#examples)
- [Best Practices](#best-practices)

```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/v0/api/use-script-trigger-interaction#webpage","@type":"WebPage","description":"API documentation for the useScriptTriggerInteraction function.","name":"useScriptTriggerInteraction","url":"https://scripts.nuxt.com/docs/v0/api/use-script-trigger-interaction","breadcrumb":{"@id":"https://scripts.nuxt.com/docs/v0/api/use-script-trigger-interaction#breadcrumb"},"isPartOf":{"@id":"https://scripts.nuxt.com/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://scripts.nuxt.com/docs/v0/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/v0/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":"V0","item":"https://scripts.nuxt.com/docs/v0","position":3},{"@type":"ListItem","name":"API","item":"https://scripts.nuxt.com/docs/v0/api","position":4},{"@type":"ListItem","name":"useScriptTriggerInteraction","item":"https://scripts.nuxt.com/docs/v0/api/use-script-trigger-interaction","position":5}]},{"@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.
