Track custom events in code
In the plugin: Tracking Nexus → Custom events
On this page
Custom events let your own code report things the plugin can't see by itself, such as a video finishing or a calculator being used. They go through the same consent checks and reach the same platforms as the built-in events. If a button, link or page is all you need, you can do it without code: see Add your own events without code.
Before you start
- The Custom events feature must be on (Tracking Nexus → Get started).
- Each platform must take them: on a platform's Events tab, the row Custom events (developer API) decides whether it receives your events.
- Tracking must be on and the visitor must have allowed the platform's purpose. Events fired before the visitor has made a choice are not kept: a later "yes" doesn't reach back to them.
From JavaScript
// Name, then optional parameters. Returns the event ID, or '' if the name was refused.
TrackingNexus.track('video_complete', { video_title: 'Welcome tour', seconds: 94 });
The runtime loads deferred, so TrackingNexus is ready by DOMContentLoaded, and it doesn't exist at all on pages where the plugin loads nothing (tracking off, a staff role, no platform connected). Code that might run earlier, or on such pages, can use the queue instead, which works before the script has loaded:
(window.tnq = window.tnq || []).push(['video_complete', { video_title: 'Welcome tour' }]);
From PHP
do_action( 'tracking_nexus_track', 'newsletter_signup', array( 'placement' => 'footer' ) );
How the event travels depends on where it is fired:
- While a page is being built (before
wp_footer), the event is printed with that page, up to 10 per page. If the page is cached, every visitor gets the same event, so use this only for events that describe the page itself. - In an AJAX, REST or form-post request, the event waits in a short-lived cookie (
tn_evt, 10 minutes, the newest 3) and is sent on the visitor's next page. The cookie is set only for a visitor whose consent allows some tracking.
Names
A name starts with a letter, then letters, digits or underscores, to 40 characters in all. It can't begin with ga_, google_ or firebase_. Anything else is ignored without an error.
Names that belong to the built-in events are refused for custom events (compared without regard to case): purchase, refund, generate_lead, conversion, page_view, page, pageload, pagevisit, checkout, signup, sign_up, login, first_visit, first_open, session_start, user_engagement, app_remove, error, download, addtocart, viewcategory, watchvideo, initiatecheckout, CompletePayment, PlaceAnOrder, SubmitForm, ApplicationApproval and Meta's standard event names (such as Lead, Contact, Search, ViewContent, AddToCart). Leads and purchases come only from your form and shop integrations, never from this API.
If the name matches one of the plugin's own browsing events (for example contact, search or add_to_cart), it is sent as that event instead of a custom one, and follows that event's switches.
Parameters
- Up to 25 parameters. Names follow the same rules as event names.
- Values can be strings, numbers (finite) or booleans. Anything else is dropped.
- Strings are cut to 100 characters. A string that looks like an email address is dropped, because platforms forbid them.
What each platform sees
Each platform you have switched Custom events (developer API) on for gets the event under your name. With Server copies of browsing events on, your events also go from your server, under the same event ID, for the platforms that take them. With the data layer on, the event is pushed as { event: 'your_name', event_id: '…', tn: { …params } }.
Check it works
Add ?tn_debug=1 to a page address and open the browser console. Every event and consent decision is logged, including the platforms an event was sent to. Then check Activity for server copies.
Questions
Why doesn't my custom event show up?
Usually the name is invalid or reserved, the Custom events feature is off, the platform's Custom events (developer API) switch is off, or the visitor hasn't allowed that purpose yet. TrackingNexus.track() returns an empty string when it refuses the name. An event fired before the visitor has chosen still returns an ID, but it isn't kept.
Can I send a lead or purchase from code?
No. generate_lead, purchase and page_view are refused: leads come from your form plugin and purchases from WooCommerce.
Will an event fired in PHP repeat on a cached page?
Yes, if it is printed with the page: a page cache serves the same page to everyone. Fire it from an AJAX or form-post request instead, or from JavaScript on the event itself.
Can I include a visitor's email in a parameter?
No. A value that looks like an email address is dropped.
Last updated 27 November 2026