npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

9466-utm-tracker

v2.0.1

Published

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

Readme

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

Cookie 存储

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