How to Create an Affiliate Program with Drupal | AffiliateBase

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:

1

Add the Tracking Script

Add the AffiliateBase tracking script to your Drupal site. You can add it via your theme's html.html.twig template or using a module like "Asset Injector". Using jsDelivr CDN ensures fast global delivery and automatic updates. No hosting required.
html
<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>
2

Add via Theme Template

Alternatively, add the script directly to your Drupal theme's html.html.twig file before the closing tag.
twig
{# 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>
3

Add Stripe Payment Links Support (Optional)

If you're using Stripe Payment Links or Buy Buttons on your Drupal site, add this script to automatically pass the referral ID.
html
<!-- 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>
4

Integrate with Drupal Commerce (Advanced)

For Drupal Commerce, capture the referral ID at checkout and pass it to Stripe.
php
<?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',
    ];
  }
}
5

Test Your Integration

Visit your Drupal site with a test affiliate link and verify tracking is working.
javascript
// 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?
Yes! You can use Drupal hooks to capture the referral ID at checkout and pass it to your payment processor.
Do I need a custom module for AffiliateBase?
For basic tracking, no module is needed. Just add the script tag. For advanced Commerce integration, a simple custom module helps.
How do I add the script without editing templates?
Use the Asset Injector module to add custom scripts without modifying theme files.
What tracking parameters does AffiliateBase support?
AffiliateBase automatically detects ?via=, ?ref=, ?affiliate=, and ?a= URL parameters.

Ready to Launch Your Drupal Affiliate Program?

Get started with AffiliateBase today and start growing your Drupal site with affiliate marketing.