Custom events
Send events with properties, with or without code
Page views come on their own. A custom event records something that matters to your business: a signup, a purchase, a click on a pricing plan. Send one from your code with track, or from your HTML with a class, and add properties to say more about it.
Send an event
Give the event a name and, if you like, properties:
1import { track } from "@viakite/sdk";23track("Signup", { plan: "pro", source: "pricing" });
1viakite.track("Signup", { plan: "pro", source: "pricing" });
With the npm package or a framework component, calls made before Viakite has started are kept and sent once it does. With the script tag, the script loads with defer; to call it earlier, add this stub above the script tag and use the function form until the script has loaded:
1<script>2window.viakite = window.viakite || function () { (window.viakite.q = window.viakite.q || []).push(arguments) };3</script>4<script defer src="https://us.api.vk.viabio.me/v1/a.js" data-project="pk_live_us_…"></script>5<script>6viakite("track", "Signup", { plan: "pro" });7</script>
Events are sent at once and show in Analytics → Events and in Realtime within seconds. track never throws and never blocks your page; a failed send is retried after 1, 5 and 15 seconds.
Properties
Properties are flat key and value pairs. A value is text, a number, true/false or null. Objects and lists are dropped, so flatten them:
1// Not sent: address is an object2track("Order placed", { address: { city: "Pune" } });34// Sent5track("Order placed", { address_city: "Pune", items: 3, gift: false });
Name events and properties the way you want to read them in reports. Keep the same name for the same thing: Signup and signup are two different events.
E-mail addresses, phone numbers and long tokens found in property values are replaced with [email], [phone] and [token] before anything is stored. See Privacy.
Limits
| Limit | |
|---|---|
| Event name | 1 to 120 characters |
| Properties | Up to 20 per event |
| Property names | 1 to 40 characters: letters, digits, _, -, . |
| Property values | Text up to 255 characters, a number, true/false or null |
revenue and currency | A number and a 3-letter ISO code; they don't count against the 20. See Revenue |
Values over a limit are cut, never rejected; anything that can't be sent is dropped. The SDK and the server apply the same rules. With data-debug each cut or drop is a warning in the browser console, and the Last events list on the Install page shows the server's warnings next to each event.
Events without code
A class on any element sends an event when it is clicked. This works with the script tag and with every component, with no JavaScript of your own:
1<button class="viakite-event-name=Signup viakite-event-plan=pro">Sign up</button>
viakite-event-name=… names the event; every other viakite-event-<key>=<value> class becomes a property. Write a space as +: viakite-event-name=Sign+up. The class can sit on the clicked element or on any of its parents. Left and middle clicks count.
Automatic events
Two events are sent for you, on by default:
| Event | When | Property | Turn off |
|---|---|---|---|
Outbound link | A click on a link to another site | url: the link, without its query and fragment | data-outbound="false" or outbound={false} |
File download | A click on a link to a file | url: the file's address | data-downloads="false" or downloads={false} |
A link counts as a download when its path ends in one of these extensions: pdf, zip, rar, 7z, dmg, apk, exe, msi, doc, docx, xls, xlsx, pptx, csv, mp4, mov, mp3. Replace the list with data-download-ext="pdf,zip,apk" (or the downloadExtensions option).
Broken pages
To mark your not-found page, add data-404 to its script tag (or notFound to its component, or call pageview({ notFound: true })). Its page view is then stored as a broken page. A custom event named 404 is marked the same way.
Broken pages show in Analytics → Broken pages, with the pages that linked to them.
No reports for these yet
Outbound link and File download show as event names on the Events page, but have no report of their own yet.