From 973fc8e076c77e58acd8ec3a634fa9991a1a1021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sadjow=20Le=C3=A3o?= Date: Wed, 11 Feb 2026 11:55:13 -0300 Subject: [PATCH 1/4] feat: add hooks mechanism for extensible tracking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Luis David Barrera Díaz --- README.md | 122 ++++++++++++- eslint.config.mjs | 1 + src/module.ts | 18 +- src/runtime/composables.ts | 10 +- src/runtime/plugin.ts | 22 ++- test/fixtures/hooks/app.vue | 13 ++ test/fixtures/hooks/nuxt.config.ts | 5 + test/fixtures/hooks/package.json | 5 + .../hooks/plugins/hooks-helper.client.ts | 57 ++++++ test/integration-hooks.test.ts | 171 ++++++++++++++++++ types/index.d.ts | 9 + 11 files changed, 417 insertions(+), 16 deletions(-) create mode 100644 test/fixtures/hooks/app.vue create mode 100644 test/fixtures/hooks/nuxt.config.ts create mode 100644 test/fixtures/hooks/package.json create mode 100644 test/fixtures/hooks/plugins/hooks-helper.client.ts create mode 100644 test/integration-hooks.test.ts diff --git a/README.md b/README.md index f80033c..f494979 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ If a visitor arrives at a website that uses the Nuxt UTM module and a UTM parame - **📍 UTM Tracking**: Easily capture UTM parameters to gain insights into traffic sources and campaign performance. - **🔍 Intelligent De-duplication**: Smart recognition of page refreshes to avoid data duplication, ensuring each visit is uniquely accounted for. - **🔗 Comprehensive Data Collection**: Alongside UTM parameters, gather additional context such as referrer details, user agent, landing page url, browser language, and screen resolution. This enriched data empowers your marketing strategies with a deeper understanding of campaign impact. +- **🔌 Hooks & Extensibility**: Three runtime hooks (`utm:before-track`, `utm:before-persist`, `utm:tracked`) let you skip tracking, enrich data with custom parameters, or trigger side effects after tracking completes. ## Quick Setup @@ -86,6 +87,9 @@ const utm = useNuxtUTM() // - enableTracking(): Enable UTM tracking // - disableTracking(): Disable UTM tracking // - clearData(): Clear all stored UTM data +// - onBeforeTrack(cb): Hook called before data collection +// - onBeforePersist(cb): Hook called to enrich/modify collected data before saving +// - onTracked(cb): Hook called after data is saved ``` @@ -125,11 +129,7 @@ const rejectTracking = () => {

Privacy Settings

-
+

Custom Hook Testing

+
+ + +
+

Try visiting with UTM parameters:

Add UTM params to URL +

+ Then click a custom hook button above and check + customParams.pageCategory in collected data. +

@@ -51,6 +61,15 @@ import { useNuxtUTM } from '#imports' const utm = useNuxtUTM() + +const visitWithPageCategory = (pageCategory) => { + const url = new URL(window.location.href) + url.searchParams.set('utm_source', 'playground') + url.searchParams.set('utm_medium', 'manual-test') + url.searchParams.set('utm_campaign', 'custom-hook') + url.searchParams.set('page_category', pageCategory) + window.location.href = `${url.pathname}?${url.searchParams.toString()}` +}