Skip to main content
Best answer

How can I use different metrics of Google Analytics 4 output? Splitting text doesn't work.


Flare

Google Analytics is returning metrics values as follows:

 

However when trying to use this in a later step, it doesn’t allow me to use value by value:

 

Even when I try splitting that output, I get the same issue in a later step:

How can I solve this?

Best answer by FlareBest answer by Flare

Fixed it thanks to ChatGPT. Here’s the solution for reference in case anyone else needs this.

Input:

Script:

// Function to split a comma-separated string into an array of numbers
function splitAndParse(str) {
  return str.split(',').map(Number);
}

// Function to split a comma-separated string into an array of strings
function splitString(str) {
  return str.split(',');
}

// Split the strings into arrays
const past7days = splitAndParse(input.past7days);
const previous7days = splitAndParse(input.previous7days);
const metricNames = splitString(input.metricnames);

output = {};

// Assign values from past7days array to the output object based on metricNames
for (let i = 0; i < past7days.length; i++) {
  output[`${metricNames[i]}_past7days`] = past7days[i];
}

// Assign values from previous7days array to the output object based on metricNames
for (let i = 0; i < previous7days.length; i++) {
  output[`${metricNames[i]}_previous7days`] = previous7days[i];
}
 
View original
Did this topic help you find an answer to your question?
This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

11 replies

jayeshkumarbhatia
Forum|alt.badge.img+6

Hi @Flare ,

 

What is the expected output do you want? Do you want to sum all the numbers and than send to Slack?


Flare
  • Author
  • Beginner
  • 6 replies
  • August 16, 2023

Hey @jayeshkumarbhatia! I want the numbers separately (value by value), not in comma separated blob.


jayeshkumarbhatia
Forum|alt.badge.img+6

You can use Looping by Zapier, that should solve your problem. You can add Looping by Zapier action just above Slack Send Message action.


Flare
  • Author
  • Beginner
  • 6 replies
  • August 16, 2023

No, I don’t want to send a bunch of Slack messages. I want to use all numbers in one Slack message.


jayeshkumarbhatia
Forum|alt.badge.img+6

Can you show me/share screenshot on the expected output that you want? 


Flare
  • Author
  • Beginner
  • 6 replies
  • August 16, 2023

 

I have two steps that get Google Analytics data (from past 7 days & from 7 days previous to those). I want those all in one Slack message. 


Flare
  • Author
  • Beginner
  • 6 replies
  • August 17, 2023

Anyone? :)


jayeshkumarbhatia
Forum|alt.badge.img+6

Hi @Flare , You can write a Javascript code to extract the Google Analytics value from the array and create Slack message they way you want


Flare
  • Author
  • Beginner
  • 6 replies
  • August 17, 2023

Wouldn’t it be easier if the text formatter actually did what it’s supposed to do and deliver an output that is usable?


Flare
  • Author
  • Beginner
  • 6 replies
  • Answer
  • August 18, 2023

Fixed it thanks to ChatGPT. Here’s the solution for reference in case anyone else needs this.

Input:

Script:

// Function to split a comma-separated string into an array of numbers
function splitAndParse(str) {
  return str.split(',').map(Number);
}

// Function to split a comma-separated string into an array of strings
function splitString(str) {
  return str.split(',');
}

// Split the strings into arrays
const past7days = splitAndParse(input.past7days);
const previous7days = splitAndParse(input.previous7days);
const metricNames = splitString(input.metricnames);

output = {};

// Assign values from past7days array to the output object based on metricNames
for (let i = 0; i < past7days.length; i++) {
  output[`${metricNames[i]}_past7days`] = past7days[i];
}

// Assign values from previous7days array to the output object based on metricNames
for (let i = 0; i < previous7days.length; i++) {
  output[`${metricNames[i]}_previous7days`] = previous7days[i];
}
 

SamB
Community Manager
Forum|alt.badge.img+11
  • Community Manager
  • 7472 replies
  • August 22, 2023

That’s awesome news, @Flare! 🎉 Thank you so much for sharing details of the solution you found - I’m sure it'll be invaluable to other folks in the Community here! 🤗