Created an extension for adding the scripts for Google Tag Manager and Google Analytics 4 as updated from Google Analytics.
For the updated version of the Google tag, we need to update the script in our SuiteCommercer Advanced Website.
And we need to update the Google tag manager script and Google Analytics both on our website.
Without that, we’ll not be able to get the purchase data in the NetSuite version 2022.1.0 and previous versions. Since in the NetSuite Version 2023.1.0, there is an additional tab Google Analytics 4 on the Configuration record of the website so we update the GA4 id directly there but for old versions, we need to add the script manually.
Google Tag Manager Script
jQuery('head').append(
jQuery('<script>').text(
"(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})" +
"(window,document,'script','dataLayer','your_GTM_ID');"
)
);
GA4 Script :
jQuery('head').append(
jQuery('<script>').attr('async', true).attr('src', 'https://www.googletagmanager.com/gtag/js?id=your_GA4_Id ')
);
jQuery('head').append(
jQuery('<script>').text(
"window.dataLayer = window.dataLayer || [];\n" +
"function gtag(){dataLayer.push(arguments);}\n" +
"gtag('js', new Date());\n" +
"gtag('config', 'your_GA4_Id');"
)
);
Also, you can get the script in your Google Account by following the path mentioned in the Google account.
By adding these scripts to our Website we can get the data in our GA4 account with all types of events and tag on the GA4 tab.
if (window.location.href.includes('confirmation')) {
let liveOrder = LiveOrderModel.getInstance();
let lastOrderId = liveOrder.attributes.confirmation.tranid;
let itemList = liveOrder.attributes.confirmation.attributes.lines.models;
let subtotal = liveOrder.attributes.confirmation.summary.total;
let coupon = liveOrder.attributes.confirmation.summary.giftcertapplied;
let shippingCost = liveOrder.attributes.confirmation.summary.shippingcost;
let taxTotal = liveOrder.attributes.confirmation.summary.taxtotal;
let location_id = window.location.pathname;
let items = [];
for (let i = 0; i < itemList.length; i++) {
let itemA = itemList[i];
let SKU = itemA.attributes.item.attributes.itemid;
let itemName = itemA.attributes.item.attributes.displayname;
let desc = itemA.attributes.item.attributes.storedisplayname2;
let quantity = itemA.attributes.quantity;
let unitP = itemA.attributes.rate;
items.push({
"location_id": location_id,
"Affiliation": "The Book Exchange",
"item_name": itemName,
"item_id": SKU,
"price": unitP * quantity,
"item_brand": desc,
"quantity": quantity
})
}
var scriptContent = 'function gtag(){dataLayer.push(arguments);} ' +
'window.dataLayer = window.dataLayer || []; ' +
'gtag("event", "purchase", { ' +
'transaction_id:"' + lastOrderId + '",' +
'value:' + subtotal + ',' +
'tax:' + taxTotal + ',' +
'shipping:' + shippingCost + ',' +
'currency:"USD",' +
'coupon:' + coupon + ',' +
'items:' + JSON.stringify(items) +
'});';
var scriptElement = document.createElement('script');
scriptElement.type = 'application/javascript';
scriptElement.innerHTML = scriptContent;
document.head.appendChild(scriptElement);
}
}
This is the purchase event that I’ve added to the website and there are more recommendations by Google we need to add on the website in order to get the data of different pages like Add to the Cart page, Item page list, Product details Page, etc.
We also need to create the tag and variable in our Google accounts for triggering the events.
We need to append this on the head of the website and we need to add NoScript to the body of the website in case of any website has enabled the javascript blocker in their website we need to avoid that for the tracking so that it’ll not become a barrier of the tracking.
jQuery('body').append(
'<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=your_GTM_id" ' +
'height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>'
);
Main script for GA4 and Google Tag Manager(GTM)

NoScript for Google Tag Manager(GTM)

Additionally, we can add the GTM id and GA4 id in the Configuration record.
The domain you can add the domain that you’re using for the update.
