
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
plausible-tracker
Advanced tools
Unofficial frontend library to interact with Plausible Analytics.
To install, simply run:
npm install plausible-tracker
yarn add plausible-tracker
To begin tracking events, you must initialize the tracker:
import Plausible from 'plausible-tracker'
const plausible = Plausible({
domain: 'my-app.com'
})
Plausible()
accepts some options that you may want to provide:
Option | Type | Description | Default |
---|---|---|---|
domain | string | Your site's domain, as declared by you in Plausible's settings | location.hostname |
hashMode | bool | Enables tracking based on URL hash changes. | false |
trackLocalhost | bool | Enables tracking on localhost. | false |
apiHost | string | Plausible's API host to use. Change this if you are self-hosting. | 'https://plausible.io' |
The object returned from Plausible()
contains the functions that you'll use to track your events. These functions are:
trackPageview()
: Tracks a single page view.trackEvent()
: Tracks custom events and goalsenableAutoPageviews()
: Enables automatic page view tracking for SPAsFor the complete documentation on these functions and their parameters, check out the reference documentation.
To track a page view, use the trackPageview
function provided
import Plausible from 'plausible-tracker'
const { trackPageview } = Plausible()
// Track a page view
trackPageview()
You may also override the values you provided when initializing the tracker by passing a similar object as the first parameter.
This object takes the same options as the initialization one, plus the following:
Option | Type | Description | Default |
---|---|---|---|
url | string | Current page's URL. | location.href |
referrer | string or null | Referrer's address | document.referrer |
deviceWidth | number | User's device width for device tracking. | window.innerWidth |
import Plausible from 'plausible-tracker'
const { trackPageview } = Plausible({
// Track localhost by default
trackLocalhost: true,
})
// Override it on this call and also set a custom url
trackPageview({
trackLocalhost: false,
url: "https://my-app.com/my-url"
})
The second parameter is an object with some options similar to the ones provided by the official Plausible script.
The only supported option at the moment is callback
– a function that is called once the event is logged successfully.
import Plausible from 'plausible-tracker'
const { trackPageview } = Plausible()
// And override it on this call
trackPageview({}, { callback: () => console.log("Done!") })
If 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.
import Plausible from 'plausible-tracker'
const { enableAutoPageviews } = Plausible()
// This tracks the current page view and all future ones as well
enableAutoPageviews()
If your app uses URL hashes to represent pages, set hashMode
to true
:
import Plausible from 'plausible-tracker'
const { enableAutoPageviews } = Plausible({
hashMode: true
})
// Hash changes will also trigger page views
enableAutoPageviews()
The 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.
When 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()
:
import Plausible from 'plausible-tracker'
const { enableAutoPageviews } = Plausible()
const cleanup = enableAutoPageviews()
// ...
// Remove event listeners and restore history.pushState
cleanup()
To track goals, all you need to do is call trackEvent
and give it the name of the goal/event as the first parameter:
import Plausible from 'plausible-tracker'
const { trackEvent } = Plausible()
// Tracks the 'signup' goal
trackEvent('signup')
As with trackPageview
, you may also provide override values and a callback as the second and third parameters respectively:
import Plausible from 'plausible-tracker'
const { trackEvent } = Plausible({
trackLocalhost: false,
})
// Tracks the 'signup' goal with a different referrer and a callback
trackEvent(
'signup',
{ trackLocalhost: true },
{ callback: () => console.log('done') }
);
For the full method and type documentation, check out the reference documentation.
FAQs
Official frontend tracker to interact with Plausible Analytics
The npm package plausible-tracker receives a total of 27,042 weekly downloads. As such, plausible-tracker popularity was classified as popular.
We found that plausible-tracker demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.