Plerdy E-commerce with Google Tag Manager

Categories
Guide

Instructions on how to add a script to Google Tag Manager (GTM) for the Thank You Page in your store

Open your Google Tag Manager account.

  • Log in to GTM using your credentials.
  • Select the desired container.

  • After logging in, you will see a list of your sites/containers. Click on the container where you want to add the script.
  • Create a new tag.

  • Click on “New Tag” or “New Tag” (depending on the interface language).
  • Give the tag a name, for example, “Plerdy E-commerce Tracking”.
  • Tag configuration.

  • In the”Tag Configuration”, select “Custom HTML”.
  • Paste the script you provided into the code box:
    <script type="text/javascript" defer>
    var orderID = document.getElementById("order_id").innerText;
    var totalMoney = document.getElementById("total_money").innerText.replace('$', '');
    var quantity = document.getElementById("quantity").innerText;
    var plerdysendData = {
    'type': 'commerce',
    'data': {
    'order_id': orderID,
    'money': totalMoney,
    'quantity': quantity
    }
    }
    </script>

    Pay attention to the IDs of the elements ( order_id, total_money, quantity ). They must match the ID or class of the elements on your page.

    If you have the option to use classes instead of IDs and other currency symbols, here’s how you can modify the code:

    1. Selecting items by class:To select items by class, use document.querySelector(‘.classname’).This function returns the first element that matches the specified selector. If you have multiple elements with the same class and you want to take a specific one, you need to use document.querySelectorAll(‘.classname’)[index].
    2. Handling different currency symbols: I added a regular expression to remove all non-numeric characters (including currency signs) from the string.

    Here is the modified code:
    <script type="text/javascript" defer>
    var orderID = document.querySelector(".order_id_class").innerText;
    var totalMoney = document.querySelector(".total_money_class").innerText.replace(/[^0-9.]/g, '');
    var quantity = document.querySelector(".quantity_class").innerText;
    var plerdysendData = {
    'type': 'commerce',
    'data': {
    'order_id': orderID,
    'money': totalMoney,
    'quantity': quantity
    }
    }
    </script>

    Replace the order_id_class, total_money_class and quantity_class with the corresponding element classes on your page.

    This approach will remove both the $ and UAH signs, as well as other characters, leaving only numbers and dots (for decimal numbers). Nevertheless, be careful that your page does not contain any other characters that may interfere with the correct amount determination.

    Setting the trigger.

  • In the Triggering section, click Choose a trigger or Select a trigger (depending on the interface language).
  • Create a new trigger that will be activated on the Thank you page after the purchase is completed.

  • Select “Page View”.
  • Select “Some Page Views”.
  • Set the conditions under which the trigger will be activated (for example, the page URL contains “thank-you” or another identifier of the Thank You Page in your store).

    Save the changes.

  • Click “Save” або “Зберегти” (depending on the interface language).
  • Publish the changes.

  • Once you’ve added the tag and set the trigger, click the Submit button to publish the changes to your container.
  • Testing.

  • Use the preview mode in GTM to check that the script works correctly on your website before your users see it.
  • Now the script will run on the Thank You page every time a user completes a purchase in your store.

    Was this helpful?

    Leave a reply for "Plerdy E-commerce with Google Tag Manager"

    Your email address will not be published. Required fields are marked *