Quickstart
Install Cosembra on a live site in three steps: paste the loader snippet, track your first event, and check that it arrived.
Before you start
Section titled “Before you start”You need two things from your Cosembra account:
- A site key, starting with
pk_live_. It’s public: it’s safe to put in your page’s HTML. - Your site’s origins on the key’s allowlist. The site key only works from the origins listed for it, such as
https://www.example.com. List every origin that will send events, includingwwwand non-wwwvariants and any staging domains. Requests from any other origin are refused.
1. Add the loader snippet
Section titled “1. Add the loader snippet”Paste this into the <head> of every page, and replace pk_live_REPLACE_ME with your site key:
<script> !((w,d,u)=> {if(w.cosembra)return;var q=[],v=w.cosembra={q:q}; ['init','track','page','identify','reset','register','unregister','consent','debug','flush','use'] .forEach((m)=> {v[m]=function(){q.push([m,[].slice.call(arguments)])}}); var s=d.createElement('script');s.async=1;s.src=u;d.head.appendChild(s)}) (window,document,'https://cdn.cosembra.com/v1/cosembra.js');
cosembra.init('pk_live_REPLACE_ME', { endpoint: 'https://events.cosembra.com/v1/batch' });</script>The snippet defines window.cosembra straight away and loads the SDK asynchronously, so it never blocks your page. Anything you call before the SDK finishes loading is queued and replayed in order.
2. Track an event
Section titled “2. Track an event”Call cosembra.track() wherever something worth recording happens:
cosembra.track('item_viewed', { item_id: 'vid_88', item_type: 'video' })Event names are lower_snake_case. Properties are any JSON object. See Tracking events for naming rules and limits, and Recommended events for standard names and shapes.
When a user logs in, link their events to your own user id:
cosembra.identify(user.id) // on logincosembra.reset() // on logout3. Check that it works
Section titled “3. Check that it works”Turn on debug logging in the browser console on your site:
cosembra.debug()Then trigger an event. You should see [cosembra] track item_viewed and, within about 5 seconds, [cosembra] sent 1 event(s). In the Network tab there’s a POST to /v1/batch that returned 202.
If you see tracking disabled or a 401 or 403, see Troubleshooting.
Next steps
Section titled “Next steps”- Consent and privacy: wait for consent before storing or sending anything.
- Single-page apps: page views in React, Vue and other client-side routers.
- Sending from your server: purchases and other events that must not be forged.