Drupal is a powerful, flexible content management system used by millions of websites worldwide. If you're running a Drupal site and want to monetize it through an affiliate program, AffiliateBase makes it simple.
This comprehensive guide will walk you through everything you need to know about setting up an affiliate program with Drupal, from adding the tracking script to integrating with Drupal Commerce and Stripe.
Whether you're running an enterprise website, SaaS platform, or membership site, an affiliate program can help you grow your audience and increase revenue.
Follow these steps to integrate AffiliateBase with your Drupal site:
Add the Tracking Script
<script>(function(w,r){w._abq=w._abq||[];w[r]=w[r]||function(){(w[r].q=w[r].q||[]).push(arguments)}})(window,'affiliatebase');</script>
<script async src="https://cdn.jsdelivr.net/npm/affiliatebase-tracking@1/src/index.js" data-org-id='YOUR_ORG_ID'></script>
Add via Theme Template
{# In your theme's html.html.twig, before </head> #}
<script>(function(w,r){w._abq=w._abq||[];w[r]=w[r]||function(){(w[r].q=w[r].q||[]).push(arguments)}})(window,'affiliatebase');</script>
<script async src="https://cdn.jsdelivr.net/npm/affiliatebase-tracking@1/src/index.js" data-org-id='YOUR_ORG_ID'></script>
Add Stripe Payment Links Support (Optional)
<!-- Add to your site alongside the tracking script -->
<script>
document.addEventListener('DOMContentLoaded', function() {
setTimeout(function() {
const referralId = window.affiliatebase_referral || '';
if (!referralId) return;
// Update Stripe Payment Links
document.querySelectorAll('a[href^="https://buy.stripe.com/"]').forEach(link => {
const url = new URL(link.href);
if (!url.searchParams.has('client_reference_id')) {
url.searchParams.set('client_reference_id', referralId);
link.href = url.toString();
}
});
// Update Stripe Pricing Tables
document.querySelectorAll('stripe-pricing-table').forEach(table => {
table.setAttribute('client-reference-id', referralId);
});
// Update Stripe Buy Buttons
document.querySelectorAll('stripe-buy-button').forEach(button => {
button.setAttribute('client-reference-id', referralId);
});
}, 1500);
});
</script>
Integrate with Drupal Commerce (Advanced)
<?php
// modules/custom/affiliatebase/affiliatebase.module
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_form_alter().
*/
function affiliatebase_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Add to checkout forms
if (strpos($form_id, 'commerce_checkout') !== FALSE) {
$form['#attached']['library'][] = 'affiliatebase/tracking';
// Add hidden field for referral ID
$form['affiliatebase_referral'] = [
'#type' => 'hidden',
'#attributes' => ['id' => 'affiliatebase-referral'],
];
// JavaScript to populate the hidden field
$form['#attached']['html_head'][] = [
[
'#tag' => 'script',
'#value' => 'document.addEventListener("DOMContentLoaded", function() {
var field = document.getElementById("affiliatebase-referral");
if (field && window.affiliatebase_referral) {
field.value = window.affiliatebase_referral;
}
});',
],
'affiliatebase_checkout',
];
}
}
Test Your Integration
// Visit your site with a test affiliate link:
// https://yoursite.com?via=test123
// Open browser console and check:
console.log(window.affiliatebase_referral); // Should show "test123"
Why Use AffiliateBase with Drupal?
AffiliateBase is the perfect solution for Drupal developers who want to add affiliate marketing to their sites.
Simple Script Installation
Add a single script tag via theme template or Asset Injector module.
Commerce Integration
Works seamlessly with Drupal Commerce for tracking product purchases.
Stripe Integration
Track conversions from Stripe Payment Links, Buy Buttons, and checkout.
Enterprise Ready
Scalable solution perfect for enterprise Drupal deployments.
Frequently Asked Questions
Does AffiliateBase work with Drupal Commerce?
Do I need a custom module for AffiliateBase?
How do I add the script without editing templates?
What tracking parameters does AffiliateBase support?
Related Platform Guides
Create an Affiliate Program with Next.js
Complete guide to setting up an affiliate program in Next.js. Step-by-step integration with code examples and best practices for Next.js developers.
Create an Affiliate Program with Nuxt
Complete guide to setting up an affiliate program in Nuxt. Step-by-step integration with Nuxt 3, server routes, and composables for Vue developers.
Create an Affiliate Program with WordPress
Complete guide to setting up an affiliate program in WordPress. Step-by-step integration with WordPress themes, plugins, and WooCommerce for any WordPress site.
Create an Affiliate Program with Webflow
Complete guide to setting up an affiliate program in Webflow. Step-by-step integration with Webflow CMS, custom code, and forms for no-code developers.
Create an Affiliate Program with React
Complete guide to setting up an affiliate program in React. Step-by-step integration with React hooks, context, and components for React developers.
Ready to Launch Your Drupal Affiliate Program?
Get started with AffiliateBase today and start growing your Drupal site with affiliate marketing.