Background
⌘K

Google Tag Manager Integration

Learn how to integrate Affonso affiliate tracking using Google Tag Manager (GTM).

Silvestro
Written by Silvestro
Updated more than a month ago

Google Tag Manager (GTM) is a popular tag management system that allows you to deploy tracking scripts without modifying your website's code directly. This guide shows you how to add Affonso tracking through GTM.

Add Affonso Tracking via GTM

Step 1: Create a New Tag

  1. Log in to your Google Tag Manager account
  2. Select your container
  3. Click Tags in the left sidebar
  4. Click New to create a new tag

Step 2: Configure the Tag

  1. Click Tag Configuration
  2. Select Custom HTML as the tag type
  3. Paste the following code:
<script>
  (function() {
    var script = document.createElement('script');
    script.defer = true;
    script.setAttribute('data-affonso', 'YOUR_PUBLIC_PROGRAM_ID');
    script.setAttribute('data-cookie_duration', 'YOUR_COOKIE_DURATION');
    script.src = 'https://cdn.affonso.io/js/pixel.min.js';
    document.head.appendChild(script);
  })();
</script>
  1. Replace YOUR_PUBLIC_PROGRAM_ID with your program ID from the Affonso dashboard
  2. Set YOUR_COOKIE_DURATION to how many days the tracking should persist (e.g., 30 for 30 days)

Why this script format? GTM requires dynamically created scripts instead of static script tags. This code creates the Affonso tracking script programmatically, which works correctly within GTM's Custom HTML tag.

Step 3: Set the Trigger

  1. Click Triggering
  2. Select All Pages to fire the tag on every page
  3. Alternatively, create a custom trigger if you only want tracking on specific pages

Step 4: Save and Publish

  1. Click Save to save the tag
  2. Click Submit in the top right corner
  3. Add a version name (e.g., "Added Affonso affiliate tracking")
  4. Click Publish

If you need GDPR compliance, modify the script to include the consent attribute:

<script>
  (function() {
    var script = document.createElement('script');
    script.defer = true;
    script.setAttribute('data-affonso', 'YOUR_PUBLIC_PROGRAM_ID');
    script.setAttribute('data-cookie_duration', 'YOUR_COOKIE_DURATION');
    script.setAttribute('data-requires-consent', 'true');
    script.src = 'https://cdn.affonso.io/js/pixel.min.js';
    document.head.appendChild(script);
  })();
</script>

Then follow our GDPR integration guide to set up consent-based tracking.

If you're using GTM's built-in Consent Mode, you can configure the trigger to only fire after consent is granted:

  1. Go to Triggers and create a new trigger
  2. Select Consent Initialization - All Pages or create a custom trigger based on your consent management platform
  3. Assign this trigger to your Affonso tag instead of "All Pages"

Testing Your Integration

Using GTM Preview Mode

  1. In GTM, click Preview in the top right corner
  2. Enter your website URL
  3. Navigate through your site and verify the Affonso tag fires
  4. Check that the tag appears under "Tags Fired" on each page

Verifying Affonso Tracking

  1. Visit your website with ?atp=test appended to the URL
  2. Open browser DevTools (F12)
  3. Go to Console and type window.affonso_referral
  4. You should see the test referral data
  5. Verify the tracking appears in your Affonso dashboard

Troubleshooting

Tag Not Firing

  • Check that the trigger is set correctly (All Pages)
  • Verify the container is published, not just saved
  • Use GTM Preview mode to debug

Script Errors

  • Ensure all quotes are straight quotes (' or "), not curly quotes
  • Check that YOUR_PUBLIC_PROGRAM_ID is replaced with your actual program ID
  • Verify there are no extra spaces or line breaks in the code

Tracking Not Working

  • Check browser console for JavaScript errors
  • Verify the Affonso script is loading in the Network tab
  • Ensure your domain is correctly configured in Affonso dashboard

Next Steps

Once installed, connect your payment provider to track conversions and calculate commissions automatically. Connect your payment provider

Was this article helpful?

If you still need help, our support team is here for you.

Contact Support
bg

Ready to Scale Your SaaS?

Affonso is the easiest way to launch your own affiliate program. We take care of the technical stuff, so you can focus on growing your business.