Skip to main content

โš™๏ธ Google Tag Manager

After this guide, you will have the knowledge to implement sale conversion tracking over Google Tag Manager.

note

This guide assumes that you understand the functionality of Google Tag Manager and are familiar with JavaScript.

Due to the openness of Google Tag Manager (GTM) it is not possible to provide a script that works right out of the box. Nevertheless, the following script can be used as a guideline to keep efforts low.

Since it is most likely that you will have to customize the script, it is strongly recommended hat you read through the introduction, tracking overview and sale conversion tracking guides first.

caution

The user tracking container must be installed first before you can start using this integration.

Requirementsโ€‹

If you want to integrate sale conversion tracking with GTM, make sure the following requirements are met:

  1. A purchase trigger exists, that is fired after a purchase has happened.
  2. A fully filled enhanced ecommerce purchase object or detailed order information are available in the data layer, when the trigger fires.

Sale conversion scriptโ€‹

This script uses data from the enhanced ecommerce object to build and send a sale conversion tracking request to Linkster.

The enhanced ecommerce object is a data structure that google recommends for tracking sales data. However, this does not mean that this object is automatically present in your data layer. You/your developers have to implement it explicitly for it to be available.

caution

It is assumed that all line item prices are net priced with deducted discounts.

If your line item prices are not net prices with deducted discounts, you would need to adapt the script to your data structure. Read more about on how to calculate and transmit net prices to Linkster here.

This script should be implemented as a Tag of type Custom HTML:

<script>
var campaignId = YOUR_CAMPAIGN_ID; // set YOUR_CAMPAIGN_ID to your campaign id
var ecommerce = {{YOUR - CUSTOM - EEC - VARIABLE}}; // ecommerce gtm dataLayer variable name
var attribution = 1;
var cookieEmid = document.cookie.match(/emid\=([a-zA-Z0-9\-\_]+)/i);
var emid = window.localStorage['emid'] || (cookieEmid === null || cookieEmid === void 0 ? void 0 : cookieEmid[1]);

/* order variables */
var ordertoken = ecommerce.purchase.actionField.id;
var triggerId = 1;
var currency = ecommerce.currencyCode;
var voucherCode = ecommerce.purchase.actionField.coupon;
var basket = [];
var products = ecommerce.purchase.products || [];

/* basket item object */
/* fore ach item in basket */
products.forEach(function (product) {
basket.push({
'campaign_id': campaignId,
'attribution': attribution,
'emid': emid,
'trigger_id': triggerId,
'token': ordertoken,
'article_number': product.id,
'productname': product.name,
'category': product.category,
'amount': product.quantity,
'price': product.price,
});
});

var trackingUrl = 'https://trck.linkster.co/trck/ebasket/?json=' + encodeURIComponent(JSON.stringify(basket)) + '&currency=' + currency + '&vc=' + encodeURIComponent(voucherCode);
var req = new XMLHttpRequest(); req.withCredentials = true; req.open("GET", trackingUrl); req.send();
</script>

It expects that the data structure behind {{YOUR - CUSTOM - EEC - VARIABLE}} looks as follows:

{
"currencyCode": "EUR",
"purchase": {
"actionField": {
"id": "1001", // Shop order id
"coupon": "SAVE20" // Applied voucher code (if present)
},
"products": [
{
"name": "Linkster T-Shirt",
"id": "3141592", // Product id
"price": "20.16", // Net price with deducted discounts!
"category": "Clothing",
"quantity": 1
},
{
"name": "Linkster Hoodie",
"id": "1618033",
"price": "40.33",
"category": "Clothing",
"quantity": 1
}
]
}
}

Testing the integrationโ€‹

It is always best practice to test your work. Testing your sale conversion script consists of four simple steps.

  1. Start a new debugging session from within GTM.

  2. Open your browser's network tab (Right click > Inspect > Network)

    2.1. If you see a checkbox named Preserve log, make sure it is checked.

    Chrome network tabChrome network tab

  3. Make a test purchase in the debugging session.

  4. Check in your browser's network tab/tool if an HTTP GET request has been sent to https://trck.linkster.co/trck/ebasket/ and make sure it contains all required attributes. The status of the request should be in the okay range (200 - 299) and under Payload you should see all your order information in a structure looking like this:

    Chrome network tabChrome network tab

If you don't see this request at the end of your order process, it means that there is still at least one error in your integration. You can check the troubleshooting section for more information.

Troubleshootingโ€‹

No HTTP request was sentโ€‹

If no HTTP request was sent, this could either be because the set trigger did not fire or the sale conversion script threw an error.

Check in your GTM debugging tab if the sale conversion script tag was fired in the first place.

If the script was fired, but you don't see a request in the network tab, it is most likely that the sale conversion script threw an error. In this case, check the error message in your browser's JavaScript console.

Possible error causesโ€‹

  • GTM couldn't find the {{YOUR - CUSTOM - EEC - VARIABLE}}. Maybe you have misspelled it?

  • The data structure of {{YOUR - CUSTOM - EEC - VARIABLE}} is incompatible with your sale conversion script.

    Make sure that the object paths you are accessing in code are present in the custom variable. For example:

    ...
    var ecommerce = {{YOUR - CUSTOM - EEC - VARIABLE}};
    ...
    /**
    * For this line to work, {{YOUR - CUSTOM - EEC - VARIABLE}} needs to have at least the following structure:
    * {
    * purchase: {
    * actionField: {
    * coupon: "SAVE20"
    * }
    * }
    * }
    */
    var voucherCode = ecommerce.purchase.actionField.coupon;
    ...

Incomplete request payload / missing dataโ€‹

If your request payload has not all the required attributes or some keys of the required basket data are missing, check if your {{YOUR - CUSTOM - EEC - VARIABLE}} contains the needed data and check if your sale conversion script access this data correctly. See possible error causes from above for more information.