Personalized checkout and custom promotions with Shopify Scripts
Hello -
I’m looking to find a solution to redirect a customer back to the original referral site after they make a purchase.
Example: customer visit our Shopify store from website X, after the customer makes a purchase on our website store. Once they reach the Thank You page our Shopify store will automatically redirect them back to the original referral site.
Hello @Barjos
That kind of automatic redirect isn’t built in, but you can achieve it by capturing the referral site on arrival and then injecting a bit of JavaScript on your Thank You page to send them back. Here’s what to do next:
1. Capture the original referrer
In your theme’s <head> (e.g. in theme.liquid), add a script that on first visit checks document.referrer and, if it isn’t your own domain, saves it into a cookie or localStorage.
<script>
if (!document.cookie.includes('orig_ref=')) {
const ref = document.referrer;
if (ref && !ref.includes(location.hostname)) {
document.cookie = `orig_ref=${encodeURIComponent(ref)}; path=/;`;
}
}
</script>
2. Add your redirect script to the Thank You page
In Shopify Admin, go to Settings → Checkout and scroll down to Order processing > Additional scripts.
Paste a script that reads your orig_ref cookie and does a window.location redirect after a short delay:
<script>
(function() {
const match = document.cookie.match(/orig_ref=([^;]+)/);
if (match) {
const url = decodeURIComponent(match[1]);
setTimeout(() => { window.location.href = url; }, 3000);
}
})();
</script>
3. Test your flow
Visit your store via a test referral link (e.g. from a staging page), complete a purchase in your development store, and confirm the Thank You page redirects back after a few seconds.
4. Fallback messaging
Because some browsers block redirects or customers may find sudden redirects confusing, consider adding a note on your Thank You page like “You’ll be redirected back to [Partner Site] in 3 seconds…” so they understand what’s happening.
5. Consider an app or Plus upgrade
If you don’t want to manage cookies and scripts yourself, there are “Thank You page redirect” apps in the Shopify App Store that automate this.
On Shopify Plus, you could also customize checkout.liquid directly for a cleaner implementation without relying on Additional scripts.
Thank you for the replay and sorry if I wasn’t to clear but I wanted to specify. So the goal is only to redirect a customer back to the referral website from 1 specific website. Do I need to provide them with a specific URL to use so we can capture the exact site referral?
also is there app you recommend if that’s the easier / cleaner solution. The few I tried seem to not offer a solution I needed.
June brought summer energy to our community. Members jumped in with solutions, clicked ...
By JasonH Jun 5, 2025Learn how to build powerful custom workflows in Shopify Flow with expert guidance from ...
By Jacqui May 7, 2025Did You Know? May is named after Maia, the Roman goddess of growth and flourishing! ...
By JasonH May 2, 2025