How to add GA4(Google Analytics4) Script on the website for different events and tags.

For adding the script on the head of the website you need to first implement the GA4 on the NetSuite and need to configure the GTM id on the configuration record of the respective website.

Then we need to create a tag on the GTM for the custom events for the recommended events by Google documentation i.e. view_item, add_to_cart, remove_from_cart, add_payment_info, etc.

jQuery('head').append(                       

jQuery('<script type="application/javascript">').text(                           

'function gtag(){dataLayer.push(arguments);}' +                           

'window.dataLayer = window.dataLayer || [];' +                           

'gtag("event", "view_item", { ' +                           

'currency: " USD ",' +                           

'value: "' + totalItemP + '",' +                           

'items: [ ' +                           

' { ' +                           

'item_id: "' + SKU + '",' +                           

'item_name: "' + itemName + '",' +                           

'item_brand: "' + desc + '",' +                           

'item_category: "' + category + '",' +                           

'location_id: "' + location_id + '",' +                           

'price: "' + unitP + '",' +                           

'quantity: "' + quantity + '"' +                           

'}' +                           

']' +                           

' });'                       

)

);

We can use this format for adding the script to the website and we can add the code for different events as per the required event names for your website.

Leave a comment

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