Mangrovea

Study: ChatGPT codes your Google Tag Manager scripts

Table of contents

We had to solve the following problem on GTM (Google Tag Manager) Find a solution to retrieve part of a site's data layer and transform it into an array of variables using JavaScript code.

The dataLayer is a data layer that stores information about user interaction with a website. This information can be used by GTM to set up and trigger tags and variables, which are essential elements for tracking and managing user interaction. site traffic analysis. Solving this problem therefore involved finding a way of manipulating this data in such a way that it could be used effectively in GTM.

You can see there's a whole lot of information!

My problem was as follows: How to retrieve product IDs from this piece of code in the form [ID1, ID2, ID3...]. Not being a developer, and not wanting to disturb everyone, I tried the experiment with ChatGPT...

ChatGPT, your Google Tag Manager tracking assistant

So I decided to write in the ChatGTP console:

And here is his answer (unbelievable, isn't it?):

For those who wish, I'll put the function here.

It transforms the extract from the site's dataLayer, which contains all the information relating to the shopping cart. In my example, the variable is called "cartContent.items", into an array containing all the product IDs in the shopping cart. [PRODUCT ID 1, PRODUCT ID 2, PRODUCT ID 3...]

This array is then sent to Google so that it can understand which products have been added to the shopping cart, for remarketing purposes.

Would you like to copy and paste the function? Here it is:
For the record, we're talking about a WordPress site with a data layer generated by the GTM4WP extension.

function() {
  var cartItems = {{cartContent.items}};
  var idArray = [];
  for (var i = 0; i < cartItems.length; i++) {
    idArray.push(cartItems[i].id);
  }
  return idArray;
}

Another advantage (and not the least) is that it's super easy to rebound on the first request and ask it to do the same job with other variables.

In this example, I then ask it to do the same thing to return an array with all the product names and not the IDs this time [PRODUCT NAME 1, PRODUCT NAME 2, PRODUCT NAME 3...].

Would you like to copy and paste the function? Here it is:
Reminder: we're talking here about a WordPress site with a data layer generated by the GTM4WP extension.


function() {
  var cartItems = {{cartContent.items}};
  var nameArray = [];
  for (var i = 0; i < cartItems.length; i++) {
    nameArray.push(cartItems[i].name);
  }
  return nameArray;
}

And so on, this time with [PRODUCT QUANTITY 1, PRODUCT QUANTITY 2, PRODUCT QUANTITY 3] ... 🙂

Would you like to copy and paste the function? Here it is:
Reminder: we're talking here about a WordPress site with a data layer generated by the GTM4WP extension.

function() {
  var cartItems = {{cartContent.items}};
  var quantityArray = [];
  for (var i = 0; i < cartItems.length; i++) {
    quantityArray.push(cartItems[i].quantity);
  }
  return quantityArray;
}

ChatGPT turned out to be a practical and effective tool for solving this problem, which was relatively "simplistic" from a developer's point of view, but which would have taken me a long time to research on specialized forums.
I implemented these solutions in just a few clicks in the customer's Google Tag Manager account, then carried out a few tests and everything worked perfectly.

ChatGPT enabled me to solve a web development problem simply and efficiently, without any programming skills. This artificial intelligence (or AI), along with others of its kind such as Jasper is still in its infancy: currently in version 3, ChatGPT version 4, scheduled for release in 2024, will be much more powerful. However, we can already see the impact it can have on a company's productivity. Here I've used the tool to assist me with "code", but its scope goes far beyond that (copywriting, web copywriting, translation, marketing, project management, education, customer relations, recruitment ....).

Did you like this article?

Share it with friends and colleagues!

Latest articles

Want to know more about the latest trends in digital marketing?

Discover other thematic resources

Want to learn more about a new topic? Check out our other thematic resources.

Book a discovery call

Choose a date and time that suits you for a discovery call with our team.

Your contact
Sébastien Castelli
Co-Founder
Book a discovery call

Choose a date and time that suits you for a discovery call with our team.

Your contact
Maxime Cochini
CEO & Co-Founder
Themes: Ads
Book a discovery call

Choose a date and time that suits you for a discovery call with our team.

Your contact
Sébastien Castelli
Co-Founder
Book a discovery call

Choose a date and time that suits you for a discovery call with our team.

Your contact
Maxime Cochini
CEO & Co-Founder
Themes: Ads
Recap

The B2B marketing newsletter.
One issue per week.

Actionable 100% tips

No spam