Send events with the Silktide Script
Install the Silktide Script, send your own events with properties, record site search, and read it all back through the API.
The Silktide Script is what Analytics measures with. Once it is on a page, it records visits by itself, and your own code can send events: a form sent, a video played, a sign-up. This guide installs it, sends an event, and reads that event back through the API.
What the Analytics screens show is explained in Help: Events and Site search.
1. Install the script#
An account admin copies the snippet from Silktide, under Settings, then Silktide Script. One snippet serves the whole account: put the same one in the <head> of every page on every website you want measured. It looks like this, with your account's own address in src:
<script defer src="https://scripts.silktide.com/accounts/YOUR-ACCOUNT-KEY.js"></script>
<script>
(function (s,i,l,k,y) {
s[i] = s[i] || y;
s[l] = s[l] || [];
s[k] = function(e, p) { p = p || {}; p.event = e; p.time = Date.now(); s[l].push(p); };
s[k]('page_load');
})(window, 'stConfig', 'stEvents', 'silktide', {});
</script>Paste it exactly as Silktide gives it. defer is what keeps it from slowing your pages down; see Page speed. There is nothing to register: a website that sends visits appears in Analytics on its own.
If your site sends a Content Security Policy, allow https://scripts.silktide.com in script-src, your region's https://a.eu.silktide.com, https://a.us.silktide.com or https://a.au.silktide.com in connect-src, and the inline part of the snippet with a nonce or a hash. See Content Security Policy.
Check it is working#
Open a page with the snippet, then your browser's developer tools:
- Network shows the script loading from
scripts.silktide.com, then requests to your region'sa.address as you use the page. - Console shows a Content Security Policy error naming a Silktide address if your policy blocks it.
Within a few minutes of the first visit, the website appears as a view in the picker at the top of Analytics.
2. Send an event#
Call silktide with a name wherever the thing you care about happens:
document.querySelector("#newsletter-form").addEventListener("submit", () => {
silktide("newsletter_signup");
});The inline part of the snippet defines silktide straight away, so an event sent before the script has finished loading is queued and recorded once it has.
Choose a name that stays the same for as long as you track the event, such as newsletter_signup or video_played. Filters, goals, exports and the API all use it.
The script records some events itself, so give yours a different name from these: page_load, page_update, page_return, click, download, frustration and field.
Send properties with it#
Pass an object as the second argument to say more about what happened. Each key is a property of the event:
silktide("video_played", { video: "Campus tour", length: 95 });- Every value is recorded as text. Send text, numbers, or
trueandfalse; an object or a list inside a property is not recorded in a useful form. - You do not need to send the page's address. Every event records the page it was sent from.
event,timeandpageUrlare the script's own, so they are not recorded as properties.- Keep each property's name the same for as long as you track it.
Keep personal data out#
Never put an email address, a person's name, an account number or anything else that identifies someone into an event name or a property value. Events are counted and listed for everyone who can see Analytics, and they are kept like the rest of your analytics.
3. Record site search#
Site search needs no code. In Silktide, under Analytics, Settings, then Data, list a CSS selector for each of your site's search boxes, such as #search or input[name=q]. Saving updates the Silktide Script for you, and from then on it records what people search for. Visitors' browsers can keep the old script for up to an hour.
To also learn which searches found nothing, describe your search results page in the same place instead. Help explains both, and what is recorded before a visitor consents: Set it up.
4. Read it back#
Every Analytics query takes view (a view's id, or all) or websiteId, and a period. See Analytics for those and for filters.
List the times your event was sent today, newest first:
curl "https://api.eu.silktide.com/v1/results/analytics/event-occurrences?event=video_played&view=all&period=today" \
-H "Authorization: silktide token=$SILKTIDE_API_KEY"{
"result": {
"page": 1,
"minPage": 1,
"maxPage": 1,
"numRows": 1,
"rowsPerPage": 100,
"rows": [
{
"key": "ad1abbcae73216d5cb08fd19320283d4",
"time": 1794060780,
"url": "https://fake-university.example/visit/",
"sessionId": "6333486291072211921",
"properties": { "video": "Campus tour", "length": "95" }
}
]
},
"selector": "/v1/results/analytics/event-occurrences",
"duration": 0.03
}Each property comes back as text, so 95 is "95". An event that has not been sent yet in the period gives an empty rows, not an error.
The other queries for events and site search:
| To get | Use |
|---|---|
| Every event your site sends, with counts and when each was first and last seen | Get events |
| How often events were sent, over time | Get events over time |
| The properties one event was sent with | Get event properties |
| The values of one property, most sent first | Get event property values |
| Every time one event was sent | Get event occurrences |
| What people searched for, with searches and visitors per term | Get site search terms |
| Searches per day, for every term or one | Get site searches over time |
Results queries can answer 200 with an error body, so check for error before you read result; see Results.