Article

Tracking – Product categorization

Product categorization is an advanced integration that splits your conversions by the type of product purchased. Each category gets its own Affilae key, and the script triggers the conversion matching the products actually present in the cart.

You can therefore reward a furniture sale and a book sale differently, with one commission rule per category.

Before you start: this is an advanced integration. It requires mastery of the e-commerce DataLayer, knowledge of GTM variables and their structure, JavaScript skills for customization, and access to product IDs in your e-commerce system.

What a product categorization integration does

This integration consists of:

  • segmenting products by business categories (for example electronics, apparel, books);
  • assigning specific Affilae KEYS to each category;
  • automatically calculating amounts by category for each order;
  • triggering the appropriate conversions according to the products in the cart.
Not to be confused: this page covers the tracking of conversions by category. It does not cover the product feed, which is the catalogue file (CSV, XML or JSON) you make available to your publishers. For that topic, see Product feeds: what they are for and what your file must contain.

Technical prerequisites

Required configuration

  • An e-commerce Data Layer correctly configured.
  • The category of each product in that Data Layer, for example item_category with Google Analytics 4.
  • The Affilae tracking tag must be loaded on the confirmation page.

Two ways to install the integration

  • GTM integration (client-side): with a Custom HTML tag.
  • Custom integration (native code): direct integration in the confirmation page code.

Example code

Below is an example for two categories, plus a default key for all other products. It reads the category of each product in the GA4 Data Layer and sends one conversion per key, with the same order number.

<script>
// Send one Affilae conversion per product category found in the order.

// GTM variables
var items = {{dl_ecommerce.items}};
if (Object.prototype.toString.call(items) !== '[object Array]') { items = []; }

var orderId = gtmString('{{dl_ecommerce.transaction_id}}');
var coupon = gtmString('{{dl_ecommerce.coupon}}');
var currency = 'EUR';

// Category, exactly as in item_category of your dataLayer -> Affilae key of its rule
var keysByCategory = {
'Mobilier': 'RULE_ID_MOBILIER-PROGRAM_ID',
'Livres': 'RULE_ID_LIVRES-PROGRAM_ID'
};

// Key for products of any other category ('' to send nothing for them)
var defaultKey = 'RULE_ID_AUTRES-PROGRAM_ID';

// Add up amount and product ids per key
var conversionsByKey = {};

items.forEach(function (product) {
var category = gtmString(product.item_category);
var key = keysByCategory.hasOwnProperty(category) ? keysByCategory[category] : defaultKey;
if (!key) return;

if (!conversionsByKey[key]) {
conversionsByKey[key] = { amount: 0, products: [] };
}
conversionsByKey[key].amount += toNumber(product.price) * toQuantity(product.quantity);

var productId = gtmString(product.item_id);
if (productId) conversionsByKey[key].products.push(productId);
});

// No order id -> the tracking endpoint rejects the hit anyway, do not fire
if (orderId) {
Object.keys(conversionsByKey).forEach(function (key) {
if (conversionsByKey[key].amount > 0) {
sendAffilae(key, conversionsByKey[key].products, conversionsByKey[key].amount);
}
});
}

// A missing GTM variable is substituted as the literal string 'undefined'
function gtmString(value) {
return (value === null || value === undefined || value === 'undefined') ? '' : String(value);
}

// Prices may arrive as strings, with a comma as decimal separator
function toNumber(value) {
var n = parseFloat(String(value === null || value === undefined ? 0 : value).replace(',', '.'));
return isFinite(n) ? n : 0;
}

// Default to 1 only when the quantity is absent, never when it is explicitly 0
function toQuantity(value) {
if (value === null || value === undefined || value === '') return 1;
var n = toNumber(value);
return n > 0 ? n : 0;
}

// Push conversion to Affilae
function sendAffilae(key, products, amount) {
var encodedProducts = [];
for (var i = 0; i < products.length; i++) {
encodedProducts.push(encodeURIComponent(products[i]));
}

var aeEvent = {
key: key,
Conversion: {
id: encodeURIComponent(orderId),
amount: amount.toFixed(2),
payment: 'online',
voucher: encodeURIComponent(coupon),
currency: currency,
product: encodedProducts.join(';')
}
};

if (('AeTracker' in window) && typeof AeTracker.sendConversion === 'function') {
AeTracker.sendConversion(aeEvent);
} else {
(window.AE = window.AE || []).push(aeEvent);
}
}
</script>

Adapting the code to your shop

  1. Create in GTM the data layer variables dl_ecommerce.items, dl_ecommerce.transaction_id and dl_ecommerce.coupon (paths ecommerce.items, ecommerce.transaction_id, ecommerce.coupon).
  2. List your categories in keysByCategory, written exactly as in your Data Layer: the comparison is case- and accent-sensitive.
  3. Map each category to the key of its rule, in the Rule ID-Program ID format, as shown in the Tracking tab.
  4. Choose defaultKey: the key of a rule covering all other products, or '' if you do not pay commission on other categories.
  5. Check the amount: each conversion receives the sum of price × quantity of its products. The price field of your Data Layer must exclude tax; a discount applied to the whole order is not deducted.
  6. Replace EUR with a currency of your rules: a currency not listed in the Rule Currencies of the rule gets the conversion refused.
  7. If your Data Layer stores the category elsewhere (for example item_category2), adapt the line reading item_category.

Where to find your keys and your IDs

  1. In the sidebar, click Programs, then your program name.
  2. Click Configuration.
  3. Open the Tracking tab.
  4. Expand the Integration via an Affilae plugin section: it shows the Program ID. Pick the rule in the list: the section shows its Rule ID, its Currency and its Key.
The Integration via an Affilae plugin section showing the Program ID, Rule ID, Currency and Key values
The Integration via an Affilae plugin section of the Tracking tab, where the values used in the code are shown.
Tip: create your commission rules first, one per category, plus one rule for defaultKey if you pay commission on other products, then collect their key. One key matches one rule: if you add a category later, you will need a new rule and a new key.

Points of attention

  • Check your DataLayer structure before installing the tag.
  • Test with real orders.
  • Verify calculated amounts and the various category triggers.
  • A product whose category is not listed goes to defaultKey, or triggers no conversion if defaultKey is empty.

Related articles

Frequently asked
questions

Check the Program ID. That is the value tying the click script to your program. You find it in Programs, your program, Configuration, Tracking tab, Integration via an Affilae plugin section.

Check three things, in this order. The variables of your dataLayer, the category names you listed in the code, and the Affilae keys used for each category. A single wrong value among those three is enough to make the conversion disappear or attach it to the wrong rule, with no error message.

In your program configuration, Tracking tab, Integration via an Affilae plugin section. It shows the Program ID, the Rule ID, the Currency and the Key. The key has the Rule ID-Program ID form.

Yes. In Affilae, an event, here the sale of a product in a given category, corresponds to a commission rule, and therefore to a key. You create as many rules as there are categories to track, plus one rule for defaultKey if you pay commission on other products.

It is sent with defaultKey. If defaultKey is empty, no conversion is triggered for that product.

No, testing is mandatory before going live. Place a real order per category, then check the calculated amounts and the trigger of each conversion. If in doubt, contact Affilae support with the details of your configurations.

Need help?

Stay up to date with the latest trends in affiliate marketing and never miss any of our articles.

Contact Us