Spotify Ads Cross-Domain Tracking | OpsBlu Docs

Spotify Ads Cross-Domain Tracking

How to track users across multiple domains and subdomains with Spotify Ads. Covers cross-domain configuration, cookie handling, session stitching, and.

Overview

Cross-domain tracking maintains Spotify attribution data across multiple domains in your conversion funnel.

Same Pixel Across Domains

Deploy the same Spotify Pixel ID on all domains:

<!-- Domain A and Domain B -->
<script type="text/javascript">
(function(s,p,o,t,i,f,y){s['SpotifyPixelObject']=i;s[i]=s[i]||function(){
(s[i].q=s[i].q||[]).push(arguments)},s[i].l=1*new Date();f=p.createElement(o),
y=p.getElementsByTagName(o)[0];f.async=1;f.src=t;y.parentNode.insertBefore(f,y)
})(window,document,'script','https://pixel.spotify.com/v1/pixel.js','spotify');

spotify('init', 'PIXEL_ID');
spotify('track', 'PageView');
</script>
function setSpotifyFirstPartyCookie(data) {
  const expiryDays = 90;
  const date = new Date();
  date.setTime(date.getTime() + (expiryDays * 24 * 60 * 60 * 1000));

  document.cookie = "spotify_user_id=" + data + ";expires=" + date.toUTCString() + ";path=/;domain=.example.com;SameSite=Lax;Secure";
}

function getSpotifyFirstPartyCookie() {
  const name = "spotify_user_id=";
  const cookies = decodeURIComponent(document.cookie).split(';');

  for (let cookie of cookies) {
    cookie = cookie.trim();
    if (cookie.indexOf(name) === 0) {
      return cookie.substring(name.length);
    }
  }
  return null;
}

URL Parameter Passing

function appendSpotifyParams(url) {
  const spotifyData = sessionStorage.getItem('spotify_attribution');

  if (spotifyData) {
    const data = JSON.parse(spotifyData);
    const separator = url.indexOf('?') > -1 ? '&' : '?';
    url += separator + 'spotify_source=' + data.source + '&spotify_campaign=' + data.campaign;
  }

  return url;
}

// Apply to cross-domain links
document.querySelectorAll('a[href*="checkout.example.com"]').forEach(link => {
  link.href = appendSpotifyParams(link.href);
});

Promo Code Persistence

// Store promo code across domains
function storePromoCodeGlobally(code) {
  sessionStorage.setItem('spotify_promo_code', code);
  localStorage.setItem('spotify_promo_code', code);
  setSpotifyFirstPartyCookie('promo_' + code);
}

// Retrieve on destination domain
function retrievePromoCode() {
  return sessionStorage.getItem('spotify_promo_code') ||
         localStorage.getItem('spotify_promo_code') ||
         getSpotifyFirstPartyCookie();
}

Best Practices

  • Deploy same pixel ID across all domains
  • Use first-party cookies for attribution data
  • Pass UTM parameters via URL
  • Store promo codes in sessionStorage and localStorage
  • Test cross-domain flows thoroughly