• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

123
Views
Is there a way to get multiple element inner text in a list to calculate the average for a custom variable in GTM?

The website I am scraping has a list of wait times that change regularly. I would like to have a custom js variable in Google Tag Manager if possible that I can use to push the average of these values when the event fires to Google Analytics. I have code in R that works to get it hourly but I am asked to pull it as a custom dimension with each event. I am wondering if there is a way to do a similar thing in js. Here is the R code:

er_times <- ("url") %>%
  read_html() %>%
  html_nodes(xpath="//div[@class='time-info--right']/text()")
er_times <- as_list(er_times)

That gives me a list of the inner text from all elements of 'time-info--right' class which contains the times and then I have a script that cleans the data and takes the average of the wait times.

So I am looking to get a list or similar data type in js where I could manipulate it, convert to int, and take an average, and use as a custom variable in GTM. So far I've tried:

Var wait_times = document.evaluate(‘/html/body/div[2]/div/main/div[2]/div/div[1]/div[2]/div[3]/div[1]/div[2]/div[1]/div[2]’, document, null, XPathResult.ANY_TYPE, null);

And

document.querySelectorAll(".time-info--right").innerText

which both gave me undefined.

document.querySelectorAll(".time-info--right").innerText

gives me the first item I need only.

No experience with js so if anyone could tell me if this is possible to do that would be great! Thanks!

almost 3 years ago · Santiago Gelvez
1 answers
Answer question

0

Somethings like this should work in GTM (without using constructs from ES6+). The key point is that document.querySelectorAll returns a node list. You can't just access the text property of that list, you have to iterate through it using an iterator of some kind like a for or forEach loop in order to access the text property of each individual node.

getTimes = function() {
  var timeArray = [];
  var times = document.querySelectorAll('.class-for-your-times');
  times.forEach(time => {
    var numberTime = parseInt(time.innerText);
    timeArray.push(numberTime);
  });
  return timeArray
}
almost 3 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error