Skip to content

Quickstart

Install Cosembra on a live site in three steps: paste the loader snippet, track your first event, and check that it arrived.

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, including www and non-www variants and any staging domains. Requests from any other origin are refused.

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.

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 login
cosembra.reset() // on logout

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.