← All docs

Configuration

JavaScript API

SourceTag exposes a window.__sourcetag object that gives you direct access to attribution data from JavaScript. This is useful for single-page applications, social login flows, and any custom integration where you need attribution data outside of a standard form submission.

Accessing the API

After the SourceTag script loads, window.__sourcetag is available globally. It contains the following:

window.__sourcetag = {
  data: { ... },        // Full cookie data object
  getFC: function(),     // Returns first click touch data
  getLC: function(),     // Returns last click touch data
  getVisits: function(), // Returns total visit count
  getDaysToConvert: function() // Returns days since first visit
};

Methods

window.__sourcetag.data

The complete cookie data object. Contains everything SourceTag knows about this visitor.

var data = window.__sourcetag.data;

console.log(data.fc);        // First click touch object
console.log(data.lc);        // Last click touch object
console.log(data.visits);    // Number of sessions
console.log(data.firstVisit); // Timestamp of first visit (ms)
console.log(data.lastSeen);  // Timestamp of last activity (ms)

window.__sourcetag.getFC()

Returns the first click touch object. This represents the visitor’s original touchpoint.

var fc = window.__sourcetag.getFC();

console.log(fc.channel);    // "Paid Search"
console.log(fc.d1);         // "google" (detail 1)
console.log(fc.d2);         // "summer-campaign" (detail 2)
console.log(fc.d3);         // "running shoes" (detail 3)
console.log(fc.d4);         // "ad-variant-a" (detail 4)
console.log(fc.source);     // "google"
console.log(fc.medium);     // "cpc"
console.log(fc.campaign);   // "summer-campaign"
console.log(fc.term);       // "running shoes"
console.log(fc.content);    // "ad-variant-a"
console.log(fc.lp);         // "/landing/shoes?utm_source=google..."
console.log(fc.clickId);    // "EAIaIQobChMI..."
console.log(fc.clickIdType); // "gclid"
console.log(fc.clickIds);   // { gclid: "EAIaIQobChMI..." }
console.log(fc.refDomain);  // ""
console.log(fc.ts);         // 1711670400000
console.log(fc.cp);         // { brand: "acme" } (custom params, if configured)

window.__sourcetag.getLC()

Returns the last click touch object. Same structure as getFC(), but contains data from the most recent visit with new attribution data.

var lc = window.__sourcetag.getLC();

console.log(lc.channel);  // "Organic Search"
console.log(lc.d1);       // "google.com"
console.log(lc.source);   // ""
console.log(lc.refDomain); // "google.com"

window.__sourcetag.getVisits()

Returns the total number of sessions this visitor has had. A new session starts after 30 minutes of inactivity.

var visits = window.__sourcetag.getVisits();
console.log(visits); // 4

window.__sourcetag.getDaysToConvert()

Returns the number of whole days between the visitor’s first visit and the current page load. Useful for understanding how long the consideration period was.

var days = window.__sourcetag.getDaysToConvert();
console.log(days); // 12

Use case: Social login

If visitors sign up or log in using Google, Facebook, or another social provider, there’s no traditional form submission. You can use the JS API to attach attribution data to the login event.

// After social login completes
function onSocialLoginSuccess(user) {
  var st = window.__sourcetag;
  if (!st) return;

  var fc = st.getFC();
  var lc = st.getLC();

  // Send attribution data to your backend alongside the user info
  fetch('/api/social-login', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      userId: user.id,
      email: user.email,
      attribution: {
        fc_channel: fc ? fc.channel : '',
        fc_source: fc ? fc.source : '',
        lc_channel: lc ? lc.channel : '',
        lc_source: lc ? lc.source : '',
        visits: st.getVisits(),
        days_to_convert: st.getDaysToConvert()
      }
    })
  });
}

Use case: Single-page applications (SPAs)

In React, Vue, Next.js, or similar SPA frameworks, page navigations don’t trigger full page loads. SourceTag’s form detection still works (MutationObserver catches dynamically added forms), but you might want to read attribution data at specific points in the user journey.

// React example: reading attribution data in a component
function LeadForm() {
  const handleSubmit = (formData) => {
    const tt = window.__sourcetag;

    const payload = {
      ...formData,
      st_fc_channel: tt?.getFC()?.channel || '',
      st_lc_channel: tt?.getLC()?.channel || '',
      st_visits: tt?.getVisits() || 0
    };

    fetch('/api/leads', {
      method: 'POST',
      body: JSON.stringify(payload)
    });
  };

  // ... rest of component
}

Use case: Custom JavaScript flows

For any scenario where you need to read or send attribution data outside of a standard form:

// Check if SourceTag has loaded
if (window.__sourcetag) {
  var fc = window.__sourcetag.getFC();
  var lc = window.__sourcetag.getLC();

  // Append to a dataLayer push
  window.dataLayer = window.dataLayer || [];
  window.dataLayer.push({
    event: 'SourceTag_loaded',
    st_fc_channel: fc ? fc.channel : '',
    st_lc_channel: lc ? lc.channel : '',
    st_visits: window.__sourcetag.getVisits()
  });
}

Waiting for the script to load

By default, the SourceTag script loads synchronously and window.__sourcetag is available immediately after the script tag. However, if you load the script conditionally (e.g. after cookie consent) or with the async attribute, your code may run before the script has finished loading. In that case, window.__sourcetag will be undefined. You can handle this with a simple check:

function withSourceTag(callback) {
  if (window.__sourcetag) {
    callback(window.__sourcetag);
    return;
  }

  // Poll briefly for the script to load
  var attempts = 0;
  var interval = setInterval(function() {
    attempts++;
    if (window.__sourcetag) {
      clearInterval(interval);
      callback(window.__sourcetag);
    } else if (attempts > 50) {
      clearInterval(interval); // Give up after 5 seconds
    }
  }, 100);
}

// Usage
withSourceTag(function(tt) {
  console.log('First click channel:', tt.getFC().channel);
});

Notes

  • The JS API reflects the cookie state at the time the script ran. If you need the absolute latest data, read the cookie directly.
  • getFC() and getLC() return null if there’s no cookie data (e.g. the visitor has cleared their cookies or it’s a brand new visit still being processed).
  • The API is read-only. You can’t modify the cookie data through window.__sourcetag.

Doesn't answer your question or need more help? Get in touch.