Catalog npm
Back to packageRELEASE DETAILS

1.4.0

npm

9466-utm-tracker

Vue 3 hooks for handling UTM parameters in URLs and storing them in cookies

analyticshookstrackingutmvue
Published19 Mar 2025
Direct dependencies10
Transitive dependenciesNot calculated

README

Current package documentation

9466-utm-tracker

Vue 3 hook,用于处理 URL 中的 utm_source 参数并存储到 cookie 中。

安装

npm install 9466-utm-tracker

前置要求

  • Vue 3
  • Vue Router 4

这个 hook 需要在 Vue Router 环境中使用,因为它依赖于 Vue Router 来获取 URL 参数。

使用方法

  1. 确保你的应用已经配置了 Vue Router:
// 在你的 router/index.ts 中
import { createRouter, createWebHistory } from "vue-router";

const router = createRouter({
  history: createWebHistory(),
  routes: [
    // 你的路由配置
  ],
});

export default router;
  1. 在组件中使用 hook:
import { useUtm } from "9466-utm-tracker";

export default {
  setup() {
    const { utmSource } = useUtm({
      cookieExpireDays: 30, // 可选,默认30天
      cookieDomain: ".example.com", // 可选
      cookiePath: "/", // 可选,默认'/'
    });

    // utmSource 是一个 ref 对象,包含 utm_source 的值
    console.log(utmSource.value); // 例如: 'google'
  },
};

hook 会自动:

  1. 在初始化时获取 URL 中的 utm_source 参数
  2. 如果 URL 中有 utm_source 参数,保存到 cookie 中(cookie 名: _9466_utm)
  3. 如果 URL 中没有 utm_source 参数,尝试从 cookie 中读取
  4. 当 URL 参数变化时,自动更新并保存新的 utm_source 值

支持的 URL 格式

支持以下格式的 URL:

  • Hash 模式: http://example.com/#/path?utm_source=google
  • History 模式: http://example.com/path?utm_source=google

utm_source 参数会直接存储在名为 _9466_utm 的 cookie 中,例如:

_9466_utm=google

特性

  • 🚀 自动从 URL 中获取 utm_source 参数
  • 📦 将 utm_source 保存到 cookie 中
  • 🔄 如果 URL 中没有参数,则从 cookie 中恢复
  • 💪 完整的 TypeScript 支持
  • 🎯 Vue 3 组合式 API
  • 🛣️ 支持 Vue Router 4 的 hash 和 history 模式
  • 🔒 安全的路由参数处理

API

useUtm(options?)

主函数,返回一个包含 utm_source 和相关方法的对象。

选项

interface UseUtmOptions {
  cookieExpireDays?: number; // cookie 过期天数,默认 30
  cookieDomain?: string; // cookie 域名
  cookiePath?: string; // cookie 路径
}

返回值

{
  utmSource: Ref<string>; // 响应式的 utm_source 值
  getUtmFromUrl: () => string; // 从当前 URL 获取 utm_source 的方法
  getUtmFromCookie: () => string; // 从 cookie 获取 utm_source 的方法
}

许可证

MIT

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 12

Native requirements
10 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
js-cookie
Environment
{
  "local_name": "js-cookie",
  "alias": false,
  "peer_metadata": null
}
^3.0.5runtime
vue
Environment
{
  "local_name": "vue",
  "alias": false,
  "peer_metadata": null
}
^3.5.13development
jsdom
Environment
{
  "local_name": "jsdom",
  "alias": false,
  "peer_metadata": null
}
^26.0.0development
vitest
Environment
{
  "local_name": "vitest",
  "alias": false,
  "peer_metadata": null
}
^3.0.9development
typescript
Environment
{
  "local_name": "typescript",
  "alias": false,
  "peer_metadata": null
}
^5.8.2development
vue-router
Environment
{
  "local_name": "vue-router",
  "alias": false,
  "peer_metadata": null
}
^4.5.0development
@types/node
Environment
{
  "local_name": "@types/node",
  "alias": false,
  "peer_metadata": null
}
^22.13.10development
@vue/test-utils
Environment
{
  "local_name": "@vue/test-utils",
  "alias": false,
  "peer_metadata": null
}
^2.4.6development
@types/js-cookie
Environment
{
  "local_name": "@types/js-cookie",
  "alias": false,
  "peer_metadata": null
}
^3.0.6development
@testing-library/vue
Environment
{
  "local_name": "@testing-library/vue",
  "alias": false,
  "peer_metadata": null
}
^8.1.0development
vue
Environment
{
  "local_name": "vue",
  "alias": false,
  "peer_metadata": null
}
^3.0.0peer
vue-router
Environment
{
  "local_name": "vue-router",
  "alias": false,
  "peer_metadata": null
}
^4.0.0peer

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "shasum": "9d5bded68cc6310619fe26e9f6455d84caad3a1e",
  "tarball": "https://registry.npmjs.org/9466-utm-tracker/-/9466-utm-tracker-1.4.0.tgz",
  "fileCount": 6,
  "integrity": "sha512-agCwkjQM5UP9VCawO28QtJWpQ82Rt9mYjayi4aXG5AIvmPKmt5UvnYAWxunmBPuzgjeX34j0Ni9pPqoG9S8WDg==",
  "signatures": [
    {
      "sig": "MEQCIDnqPpWs/o42CctCZd2dAsKob1Ix/RkDqlG2Qh267m54AiAKD39PdtgASCbtJZUklOLs5Jg84MH4Z18EY+0gzx41ig==",
      "keyid": "SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"
    }
  ],
  "unpackedSize": 7525
}

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 21:32 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:fcb021b26bb809a33e65d3e0b7df3e4bd5c8bf635715e168334a2ab1dec91dce

Other collected versions

2.0.119 Mar2.0.019 Mar1.3.019 Mar1.2.019 Mar1.1.119 Mar1.1.019 MarView all collected versions →