{"id":19032154,"url":"https://github.com/plausible/plausible-tracker","last_synced_at":"2025-04-13T02:25:02.574Z","repository":{"id":38243059,"uuid":"303536626","full_name":"plausible/plausible-tracker","owner":"plausible","description":"Frontend library to interact with Plausible Analytics","archived":false,"fork":false,"pushed_at":"2023-06-26T09:43:50.000Z","size":479,"stargazers_count":197,"open_issues_count":30,"forks_count":44,"subscribers_count":11,"default_branch":"master","last_synced_at":"2024-05-01T10:34:06.915Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://github.com/plausible/plausible-tracker","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/plausible.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2020-10-12T23:26:59.000Z","updated_at":"2024-04-20T18:13:38.000Z","dependencies_parsed_at":"2023-07-18T00:27:04.775Z","dependency_job_id":null,"html_url":"https://github.com/plausible/plausible-tracker","commit_stats":{"total_commits":62,"total_committers":10,"mean_commits":6.2,"dds":0.3548387096774194,"last_synced_commit":"c0b87d997d839938c23023d35bac0d6683635bbc"},"previous_names":["maronato/plausible-tracker"],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/plausible%2Fplausible-tracker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/plausible%2Fplausible-tracker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/plausible%2Fplausible-tracker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/plausible%2Fplausible-tracker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/plausible","download_url":"https://codeload.github.com/plausible/plausible-tracker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248655031,"owners_count":21140417,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T21:27:05.487Z","updated_at":"2025-04-13T02:25:02.549Z","avatar_url":"https://github.com/plausible.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Plausible Analytics Tracker\n\n[![NPM](https://flat.badgen.net/npm/v/plausible-tracker)](https://www.npmjs.com/package/plausible-tracker)  [![Bundle](https://flat.badgen.net/bundlephobia/minzip/plausible-tracker)](https://bundlephobia.com/result?p=plausible-tracker@latest)\n\n[![Build Status](https://travis-ci.com/plausible/plausible-tracker.svg?branch=master)](https://travis-ci.com/plausible/plausible-tracker) [![codecov](https://codecov.io/gh/plausible/plausible-tracker/branch/master/graph/badge.svg)](https://codecov.io/gh/plausible/plausible-tracker) [![Netlify Status](https://api.netlify.com/api/v1/badges/d29c0d49-6ba4-412b-af90-d21865eb40f2/deploy-status)](https://app.netlify.com/sites/plausible-tracker/deploys)\n\nFrontend library to interact with [Plausible Analytics](https://plausible.io/).\n\n- [Plausible Analytics Tracker](#plausible-analytics-tracker)\n  - [Features](#features)\n  - [Installation](#installation)\n  - [Usage](#usage)\n    - [Tracking page views](#tracking-page-views)\n    - [Automatically tracking page views](#automatically-tracking-page-views)\n      - [Cleaning up the event listeners](#cleaning-up-the-event-listeners)\n    - [Tracking custom events and goals](#tracking-custom-events-and-goals)\n    - [Outbound link click tracking](#outbound-link-click-tracking)\n      - [Cleaning up the event listeners](#cleaning-up-the-event-listeners-1)\n  - [Reference documentation](#reference-documentation)\n\n## Features\n- Less than 1kb!\n- Same features as the official script, but as an NPM module\n- Automatically track page views in your SPA apps\n- Track goals and custom events\n- Provide manual values that will be bound to the event\n- Full typescript support\n\n## Installation\n\nTo install, simply run:\n\n```bash\nnpm install plausible-tracker\n\nyarn add plausible-tracker\n```\n\n## Usage\n\nTo begin tracking events, you must initialize the tracker:\n\n```ts\nimport Plausible from 'plausible-tracker'\n\nconst plausible = Plausible({\n  domain: 'my-app.com'\n})\n```\n\n`Plausible()` accepts some [options](https://plausible-tracker.netlify.app/globals.html#plausibleinitoptions) that you may want to provide:\n\n| Option         | Type     | Description                                                       | Default                  |\n| -------------- | -------- | ----------------------------------------------------------------- | ------------------------ |\n| domain         | `string` | Your site's domain, as declared by you in Plausible's settings    | `location.hostname`      |\n| hashMode       | `bool`   | Enables tracking based on URL hash changes.                       | `false`                  |\n| trackLocalhost | `bool`   | Enables tracking on *localhost*.                                  | `false`                  |\n| apiHost        | `string` | Plausible's API host to use. Change this if you are self-hosting. | `'https://plausible.io'` |\n\nThe object returned from `Plausible()` contains the functions that you'll use to track your events. These functions are:\n\n- [`trackPageview()`](https://plausible-tracker.netlify.app/globals.html#trackpageview): Tracks a single page view.\n- [`trackEvent()`](https://plausible-tracker.netlify.app/globals.html#trackevent): Tracks custom events and goals\n- [`enableAutoPageviews()`](https://plausible-tracker.netlify.app/globals.html#enableautopageviews): Enables automatic page view tracking for SPAs\n\nFor the complete documentation on these functions and their parameters, check out the [reference documentation](https://plausible-tracker.netlify.app/).\n\n### Tracking page views\n\nTo track a page view, use the `trackPageview` function provided\n\n```ts\nimport Plausible from 'plausible-tracker'\n\nconst { trackPageview } = Plausible()\n\n// Track a page view\ntrackPageview()\n```\n\nYou may also override the values you provided when initializing the tracker by passing a [similar object](https://plausible-tracker.netlify.app/globals.html#plausibleoptions) as the first parameter.\n\nThis object takes the same options as the initialization one, plus the following:\n\n| Option      | Type               | Description                              | Default             |\n| ----------- | ------------------ | ---------------------------------------- | ------------------- |\n| url         | `string`           | Current page's URL.                      | `location.href`     |\n| referrer    | `string` or `null` | Referrer's address                       | `document.referrer` |\n| deviceWidth | `number`           | User's device width for device tracking. | `window.innerWidth` |\n\n\n```ts\nimport Plausible from 'plausible-tracker'\n\nconst { trackPageview } = Plausible({\n  // Track localhost by default\n  trackLocalhost: true,\n})\n\n// Override it on this call and also set a custom url\ntrackPageview({\n  trackLocalhost: false,\n  url: \"https://my-app.com/my-url\"\n})\n```\n\nThe second parameter is an object with [some options](https://plausible-tracker.netlify.app/globals.html#eventoptions) similar to the ones provided by the [official Plausible script](https://docs.plausible.io/custom-event-goals). \n\n```ts\nimport Plausible from 'plausible-tracker'\n\nconst { trackPageview } = Plausible()\n\n// And override it on this call\ntrackPageview({}, { callback: () =\u003e console.log(\"Done!\") })\n```\n\n### Automatically tracking page views\n\nIf your app is a SPA that uses JS-based routing, you'll need to use browser events to manually track page views. A built-in function `enableAutoPageviews` enables automatic tracking for you so you don't need to write custom logic.\n\n```ts\nimport Plausible from 'plausible-tracker'\n\nconst { enableAutoPageviews } = Plausible()\n\n// This tracks the current page view and all future ones as well\nenableAutoPageviews()\n```\n\nIf your app uses URL hashes to represent pages, set `hashMode` to `true`:\n\n```ts\nimport Plausible from 'plausible-tracker'\n\nconst { enableAutoPageviews } = Plausible({\n  hashMode: true\n})\n\n// Hash changes will also trigger page views\nenableAutoPageviews()\n```\n\nThe way it works is by overriding `history.pushState` and attaching event listeners to `popstate` and `hashchange` (only if you set `hashMode` to `true`). If your frontend framework uses other methods to manage navigation, you might want to write your own logic using `trackPageview` to manually trigger page views.\n\n#### Cleaning up the event listeners\n\nWhen you call `enableAutoPageviews()`, it adds some event listeners and overrides `history.pushState`. To remove them and restore `history.pushState`, call the cleanup function returned by `enableAutoPageviews()`:\n\n```ts\nimport Plausible from 'plausible-tracker'\n\nconst { enableAutoPageviews } = Plausible()\n\nconst cleanup = enableAutoPageviews()\n\n// ...\n\n// Remove event listeners and restore history.pushState\ncleanup()\n```\n\n### Tracking custom events and goals\n\nTo track goals, all you need to do is call `trackEvent` and give it the name of the goal/event as the first parameter:\n\n```ts\nimport Plausible from 'plausible-tracker'\n\nconst { trackEvent } = Plausible()\n\n// Tracks the 'signup' goal\ntrackEvent('signup')\n```\n\nCustom props can be provided using the second parameter:\n```ts\n// Tracks the 'download' goal and provides a 'method' property.\ntrackEvent('download', { props: { method: 'HTTP' } })\n```\n\nAs with [`trackPageview`](#tracking-page-views), you may also provide override values but now through the third parameter:\n\n```ts\nimport Plausible from 'plausible-tracker'\n\nconst { trackEvent } = Plausible({\n  trackLocalhost: false,\n})\n\n// Tracks the 'signup' goal with a callback, props and a different referrer.\ntrackEvent(\n  'signup',\n  {\n    callback: () =\u003e console.log('done'),\n    props: {\n      variation: 'button A'\n    }\n  },\n  { trackLocalhost: true }\n);\n```\n\n### Outbound link click tracking\n\nYou can also track all clicks to outbound links using `enableAutoOutboundTracking`.\n\nFor details on how to setup the tracking, visit the [docs](https://docs.plausible.io/outbound-link-click-tracking).\n\nThis function adds a `click` event listener to all `a` tags on the page and reports them to Plausible. It also creates a [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) that efficiently tracks node mutations, so dynamically-added links are also tracked.\n\n```ts\nimport Plausible from 'plausible-tracker'\n\nconst { enableAutoOutboundTracking } = Plausible()\n\n// Track all existing and future outbound links\nenableAutoOutboundTracking()\n```\n\n#### Cleaning up the event listeners\n\nWhen you call `enableAutoOutboundTracking()`, it adds some event listeners and initializes a `MutationObserver`. To remove them, call the cleanup function returned by `enableAutoOutboundTracking()`:\n\n```ts\nimport Plausible from 'plausible-tracker'\n\nconst { enableAutoOutboundTracking } = Plausible()\n\nconst cleanup = enableAutoOutboundTracking()\n\n// ...\n\n// Remove event listeners and disconnect the MutationObserver\ncleanup()\n```\n\n### Opt out and exclude yourself from the analytics\n\nSince plausible-tracker is bundled with your application code, using an ad-blocker to exclude your visits isn't an option. Fortunately Plausible has an alternative for this scenario: plausible-tracker will not send events if `localStorage.plausible_ignore` is set to `\"true\"`.\n\nMore information about this method can be found in the [Plausible documentation](https://plausible.io/docs/excluding-localstorage).\n\n## Reference documentation\nFor the full method and type documentation, check out the [reference documentation](https://plausible-tracker.netlify.app).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplausible%2Fplausible-tracker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fplausible%2Fplausible-tracker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplausible%2Fplausible-tracker/lists"}