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.
Select the desired container.
Create a new tag.
Tag configuration.
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:
- 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].
- 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.
Create a new trigger that will be activated on the Thank you page after the purchase is completed.
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.
Publish the changes.
Now the script will run on the Thank You page every time a user completes a purchase in your store.