Add leakproof codes to smart links (Shopify Headless)
Roster's tracking pixel includes an event system that lets brands subscribe to JavaScript callbacks when a discount code is retrieved from an ambassador's referral link. This is for headless Shopify stores that need to apply discount codes through their own cart or session mechanism.
If you're a standard Shopify store, Roster applies discount codes automatically. See Add leakproof codes to smart links (Shopify) instead.
How it works
When a customer clicks an ambassador's referral link, they land on the brand's storefront where Roster's tracking pixel fires on page load. The pixel calls Roster's discount endpoint to retrieve a single-use leakproof code associated with the ambassador's link that was clicked.
If a code is returned, the pixel fires the onCodeRetrieved callback on the window.roster object. The brand's storefront code receives the discount code in the callback and applies it through their own cart mechanism — for example, by injecting it into a session, adding it to a cart API request, or setting a discount code cookie.
Prerequisites
- Roster's tracking pixel has been installed on your storefront or website. See Pixel tracking installation to get the correct install instructions for your store or website.
- You have set up smart links for the desired program(s). See Set up smart links for detailed instructions.
Confirm that the pixel you installed is pointing to cdn.rostercdn.com/sa.js
If you see any other value (e.g. sa.getroster.com/sa.js) then you are not using our most recent pixel and need to use the CDN version instead.
Integration steps
Set up smart links for the program
See Set up smart links
Enable leakproof codes as the smart link's customer discount
- Open the customer discount section of smart link settings.
- Select leakproof codes as the discount
- Click save.

Enable discount code fetching in the tracking pixel
In your storefront or website frontend code where you installed the Roster tracking pixel, add forceShareableCodeFetch: true to the window.roster.config object (see Javascript snippet below)
This will trigger Roster's tracking pixel to attempt to create a one-time user discount code when a link click is emitted.
Define your event callbacks in your store frontend
Add an event handler for the onCodeRetrieved event, which gets called when a leakproof code is returned.
The event handler you define will be responsible for adding the discount code to the customer's cart. For example, if the code ROSTER-AKN12AGE is returned, then your handler will add that discount code to the customer's cart such that will automatically be applied during checkout.
Add the following script block demonstrates what your window.roster object should look like. The onClickTracked callback is optional.
<script type="text/javascript">
window.roster = {
config: {
token: 'YOUR_PUBLIC_ACCESS_TOKEN',
forceShareableCodeFetch: true
},
onCodeRetrieved: function(discountCode) {
console.log('Discount code:', discountCode);
// Replace this with your own logic to apply the code to the cart
},
onClickTracked: function() {
console.log('Click tracked');
},
onError: function(error) {
console.error('Roster pixel error:', error);
}
};
</script>
<script async type="application/javascript"
src="https://cdn.rostercdn.com/sa.js"></script>This script block must appear before the Roster pixel script tag in your HTML. If it appears after, the callbacks will not be registered when the pixel fires.
Download our Roster Pixel Event Tester HTML file to see how the events fire with one of your own ambassador's referral links.
Simply edit the file with your brand's public access token and paste in a link to see how the event callbacks work.
Test the integration
Once you have implemented the callbacks on your storefront or website and pushed the changes live, use an ambassador's referral link on your store to verify that the leakproof code is fetched from Roster, your event handler is called, and the discount code is applied to the shopping cart.
See Verify installation with Roster's Chrome Extension to speed up the verification process using Roster's Chrome browser extension.
Event reference
Callback | Signature | Description |
|---|---|---|
onCodeRetrieved | function(discountCode) | Called when a discount code is successfully returned from Roster's discount code endpoint. discountCode is a string containing the code to apply. |
onClickTracked | function(click) | Called when a referral click is tracked by the pixel. click is an object containing click details such as referralLinkId, landingPageUrl, referrerUrl, queryParams, utmParams, userAgent, linkSource, linkVariant, characterSet, title, location, externalId, and clickTimestamp. |
onError | function(error) | Called when an error occurs during the discount code endpoint request or a callback error is caught. error is a string or Error object describing what went wrong. |
Config reference
Property | Type | Default | Description |
|---|---|---|---|
token | string | "" | Your public access API token. Required. |
forceShareableCodeFetch | boolean | false | Set to true to enable discount code retrieval on non-Shopify storefronts. Required for this integration. |
Need help?
If you have any questions about integrating Roster's pixel event system, contact us at [email protected].
