Integration
Trackina — JS Snippet
Copy this snippet into any HTML page to start tracking page views, clicks, and form submits.
1. Basic Install (auto page view)
Paste this before </body> on every page. It will automatically track page views on load.
Minified Version
HTML (minified)
<script>!function(){var t="http://crystal.onemachina.com/api/v1/trackina/ingest",c="_trk_uuid",s="_trk_sid";function g(n){var m=document.cookie.match(new RegExp("(?:^|; )"+n+"=([^;]*)"));return m?decodeURIComponent(m[1]):null}function k(n,v,d){var e=new Date;e.setTime(e.getTime()+(d||365)*864e5);document.cookie=n+"="+encodeURIComponent(v)+";expires="+e.toUTCString()+";path=/;SameSite=Lax"}function u(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(c){var r=Math.random()*16|0;return(c==="x"?r:(r&3|8)).toString(16)})}var i=g(c)||(k(c,u(),365),g(c)),d=sessionStorage.getItem(s)||(function(){var v=u();sessionStorage.setItem(s,v);return v})();function m(){try{return new URLSearchParams(window.location.search).get("gmc")||null}catch(e){return null}}function p(o){o.cookie_uuid=i;o.session_id=d;o.url=o.url||window.location.href;o.path=o.path||window.location.pathname;o.page_title=o.page_title||document.title;o.referrer=o.referrer||document.referrer||null;o.gmc=o.gmc||m();navigator.sendBeacon?navigator.sendBeacon(t,JSON.stringify(o)):fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o),keepalive:!0})}p({event:{event_type:"page_view",event_name:document.title}});window.trackina={track:function(e){p({event:e})},pageView:function(){p({event:{event_type:"page_view",event_name:document.title}})},identify:function(c){},getCookieUuid:function(){return i},getSessionId:function(){return d}}}();</script>
HTML
<script>
(function() {
var TRK_URL = 'http://crystal.onemachina.com/api/v1/trackina/ingest';
var COOKIE_NAME = '_trk_uuid';
var SESSION_NAME = '_trk_sid';
// ── Cookie helpers ──
function getCookie(n) {
var m = document.cookie.match(new RegExp('(?:^|; )' + n + '=([^;]*)'));
return m ? decodeURIComponent(m[1]) : null;
}
function setCookie(n, v, days) {
var d = new Date(); d.setTime(d.getTime() + (days||365)*86400000);
document.cookie = n + '=' + encodeURIComponent(v) + ';expires=' + d.toUTCString() + ';path=/;SameSite=Lax';
}
function uuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0; return (c==='x'?r:(r&0x3|0x8)).toString(16);
});
}
// ── Get or create identifiers ──
var cookieUuid = getCookie(COOKIE_NAME) || (setCookie(COOKIE_NAME, uuid(), 365), getCookie(COOKIE_NAME));
var sessionId = sessionStorage.getItem(SESSION_NAME) || (function() { var s=uuid(); sessionStorage.setItem(SESSION_NAME,s); return s; })();
// ── Extract GMC from URL params ──
function getGmc() {
try { return new URLSearchParams(window.location.search).get('gmc') || null; }
catch(e) { return null; }
}
// ── Send tracking data ──
function send(payload) {
payload.cookie_uuid = cookieUuid;
payload.session_id = sessionId;
payload.url = payload.url || window.location.href;
payload.path = payload.path || window.location.pathname;
payload.page_title = payload.page_title || document.title;
payload.referrer = payload.referrer || document.referrer || null;
payload.gmc = payload.gmc || getGmc();
if (navigator.sendBeacon) {
navigator.sendBeacon(TRK_URL, JSON.stringify(payload));
} else {
fetch(TRK_URL, { method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify(payload), keepalive:true });
}
}
// ── Auto page view ──
send({ event: { event_type: 'page_view', event_name: document.title } });
// ── Public API ──
window.trackina = {
track: function(eventData) { send({ event: eventData }); },
pageView: function() { send({ event: { event_type: 'page_view', event_name: document.title } }); },
identify: function(contactId) { /* future: link cookie to contact */ },
getCookieUuid: function() { return cookieUuid; },
getSessionId: function() { return sessionId; },
};
})();
</script>
2. Track Custom Events
After the snippet is loaded, use window.trackina.track() to send events.
JavaScript
// CTA button click trackina.track({ event_type: 'cta_click', event_name: 'Watch Trailer Click', element_id: 'hero-trailer-button', link_url: 'https://cinemachina.ai/trailer', link_text: 'Watch Trailer', button_variant: 'primary', section: 'hero' }); // Form submission trackina.track({ event_type: 'waitlist_submit', event_name: 'Waitlist Form Submit', form_id: 'waitlist-form', section: 'pricing', email_domain: 'gmail.com' }); // Inline on an element <button onclick="trackina.track({event_type:'cta_click', section:'footer'})"> Sign Up </button>
3. Auto-Track with Data Attributes
Add data-trk attributes to auto-capture clicks without writing JS.
HTML
<a href="/trailer" data-trk="trailer_click" data-trk-section="hero"> Watch Trailer </a> <button data-trk="cta_click" data-trk-section="pricing" data-trk-variant="primary"> Get Started </button>
Add the auto-click listener after the snippet:
<script>
document.addEventListener('click', function(e) {
var el = e.target.closest('[data-trk]');
if (!el) return;
trackina.track({
event_type: el.dataset.trk,
event_name: el.dataset.trkName || el.textContent.trim(),
element_id: el.id || null,
link_url: el.href || null,
link_text: el.textContent.trim(),
section: el.dataset.trkSection || null,
button_variant: el.dataset.trkVariant || null,
});
});
</script>
API Reference
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/v1/trackina/ingest | Unified ingest (JS snippet uses this) |
| POST | /api/v1/trackina/cookie_history | Create a cookie record directly |
| POST | /api/v1/trackina/trackina_session | Create a session record directly |
| POST | /api/v1/trackina/trackina_session_events | Create an event record directly |