Use the sendEvent function to manually trigger an event to the Plausible API. This function requires the event name, a PlausibleOptions object containing core tracking data, and an optional EventOptions object for additional metadata.
Important Behaviors:
- Localhost Protection: By default, events are ignored if the website is running on
localhost or 127.0.0.1 unless trackLocalhost is set to true in your PlausibleOptions. - Opt-out Check: The function checks
localStorage.plausible_ignore. If this is set to 'true', the event will be ignored. - Payload Construction: The function automatically maps your options to the internal Plausible payload format, including device width and hash mode status.
import { sendEvent } from './plausible-tracker'; // Adjust import path as needed
// Example usage
sendEvent(
'button_click',
{
url: window.location.href,
domain: 'example.com',
apiHost: 'https://plausible.io',
trackLocalhost: true, // Required if testing on localhost
// ... other required PlausibleOptions
},
{
props: {
button_color: 'blue',
user_tier: 'premium'
},
callback: (args) => {
console.log('Event sent with status:', args.status);
}
}
);