macro process code by changing the parameter

Currently I use the same code to generate volume per product, I just change the parameter I have to perform this process n times.
Would it be possible to create a function that changes the parameters of the code below? Any help is much appreciated.

make v4 request

df01 <- dim_filter("PagePath","PARTIAL","_p0304/p", not = FALSE)

construct filter objects

fc_Vis <- filter_clause_ga4(list(df01), operator = "AND")

P0304 <- google_analytics_4(ga_id,
date_range = c("2020-05-11","yesterday"),
dimensions = c("channelGrouping"),
metrics = c("pageviews","uniquepageviews","users"),
dim_filters = fc_Vis,
anti_sample = TRUE)

P0304_AUX <- sqldf("SELECT
'P0304' AS ID_PRODUTO
,'TOTAL' AS channelGrouping
,SUM(pageviews) AS pageviews
,SUM(uniquepageviews) AS uniquepageviews
,SUM(users) AS users
FROM P0304")

P0304_AUX2 <-sqldf("SELECT
'P0304' AS ID_PRODUTO, *
FROM P0304
ORDER BY pageviews DESC")

P0304_CANAL <-sqldf("SELECT * FROM P0304_AUX2
UNION ALL
SELECT * FROM P0304_AUX")

make v4 request

df02 <- dim_filter("PagePath","PARTIAL","_p3235/p", not = FALSE)

construct filter objects

fc_Vis <- filter_clause_ga4(list(df02), operator = "AND")

P3235 <- google_analytics_4(ga_id,
date_range = c("2020-05-11","yesterday"),
dimensions = c("channelGrouping"),
metrics = c("pageviews","uniquepageviews","users"),
dim_filters = fc_Vis,
anti_sample = TRUE)

P3235_AUX <- sqldf("SELECT
'P3235' AS ID_PRODUTO
,'TOTAL' AS channelGrouping
,SUM(pageviews) AS pageviews
,SUM(uniquepageviews) AS uniquepageviews
,SUM(users) AS users
FROM P3235")

P3235_AUX2 <-sqldf("SELECT
'P3235' AS ID_PRODUTO, *
FROM P3235
ORDER BY pageviews DESC")

P3235_CANAL <-sqldf("SELECT * FROM P3235_AUX2
UNION ALL
SELECT * FROM P3235_AUX")

As a first step, study the section of the book kn creating your own functions

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.