Ghost's code injection feature makes it simple to add tracking scripts globally across your entire website or blog, perfect for SaaS companies using Ghost for their content marketing and knowledge base.
Add tracking script to your Ghost site
Ghost's recommended approach is to use the site-wide code injection settings, which applies the script to all pages automatically without needing to modify themes.
-
Open your Ghost Admin panel
-
Go to Settings → Code injection
-
In the Site Header section, add your tracking script:
<script async defer src="https://affonso.io/js/pixel.min.js" data-affonso="YOUR_PUBLIC_PROGRAM_ID" data-cookie_duration="YOUR_COOKIE_DURATION" ></script>
-
Click Save
The script will automatically appear on all pages and start tracking affiliate referrals.
Track Member Signups (Optional)
If you're using Ghost's built-in membership features and want to track when referred visitors become members, add this enhanced tracking code to your Site Header:
<script
async
defer
src="https://affonso.io/js/pixel.min.js"
data-affonso="YOUR_PUBLIC_PROGRAM_ID"
data-cookie_duration="YOUR_COOKIE_DURATION"
></script>
<script>
try {
// Track successful member signups
document.addEventListener('DOMContentLoaded', function() {
// Check if this is a successful signup page
if (window.location.href.includes('success') ||
window.location.href.includes('welcome') ||
document.querySelector('[data-members-success]') ||
document.querySelector('.gh-portal-success')) {
// Get member email from various sources
const memberEmail = document.querySelector('[data-member-email]')?.textContent ||
document.querySelector('.member-email')?.textContent ||
localStorage.getItem('ghost_member_email');
if (memberEmail && window.Affonso) {
window.Affonso.signup(memberEmail.trim());
}
}
// Store member email when they sign up for future tracking
const memberForms = document.querySelectorAll('[data-members-form]');
memberForms.forEach(form => {
form.addEventListener('submit', function(e) {
const emailInput = form.querySelector('input[type="email"]');
if (emailInput && emailInput.value) {
localStorage.setItem('ghost_member_email', emailInput.value);
}
});
});
});
} catch (e) {
console.error("Affonso tracking error:", e);
}
</script>
Note: This enhanced tracking automatically notifies affiliates when their referrals sign up for memberships.
For more details, see Ghost's official code injection documentation.
Testing
- Visit your Ghost site with
?atp=test
- Check browser DevTools → Console → type
window.affonso_referral
- Verify the script appears in your page source
- For enhanced tracking: Test a member signup and check your Affonso dashboard
- Check tracking in your Affonso dashboard
Note: Code injection changes apply immediately without needing to republish.
Multi-Domain Setup
If you're using Ghost only for your blog/content (e.g., blog.yourcompany.com
) while your main application runs elsewhere (e.g., app.yourcompany.com
), make sure to install the Affonso tracking script on both domains.
Affonso works seamlessly across subdomains - the same affiliate cookie will be shared between domains. See our multi-domain setup guide for more details on cross-domain tracking.
Next Steps
Once installed, you can access affiliate referral data and pass it to your payment provider. Connect your payment provider →