gpt4 book ai didi

loops - Google Analytics(分析)自然搜索循环问题

转载 作者:行者123 更新时间:2023-12-03 16:47:23 24 4
gpt4 key购买 nike

循环的功能完全不同,我不确定是否是因为使用了Google Analytics(分析)软件包,因为代码之间的差异很小。

打印语句输出中的WORK差异不会显示结果未正确输出。

library(googleAnalyticsR)
library(tidyverse)

#settings
start_dat <- as.character(Sys.Date()-31)
end_dat <- as.character(Sys.Date()-1)


#Authorize Google Analytics R- this will open a webpage
#You must be logged into your Google Analytics account on your web browser
ga_auth()

account_sum <- ga_account_list()



#Add the start and end date to the date frame, as well as some columns to use to populate the metrics
account_sum$start_dat <- start_dat
account_sum$end_dat <- end_dat

## choose the v3 segment
segment_for_call <- "gaid::-5"

## make the v3 segment object in the v4 segment object:
seg_ob <- segment_ga4("OrganicTraffic", segment_id = segment_for_call)


# cycle through the list of views, pull the data, and add it to the
#account_summary

for (i in 1:5){

view_id <- (Book1CSV[[1]][i])
views=view_id




ga_dat <- google_analytics_4(views,
date_range = c(start_dat, end_dat),
segments = seg_ob,
metrics = c("sessions", "pageviews"),
dimensions = c("year","segment"))

ga_dat <- summarise(ga_dat,
sessions = sum(sessions),
pageviews = sum(pageviews))

account_sum$sessions[i] <- ga_data$sessions
account_sum$pageviews[i] <- ga_data$pageviews
print(account_summary)
}

clean_sum <- select(account_sum,
ID = webPropertyId ,
Account = accountName,
Views = views,
Type = type,
Level = level,
'Start Date' = start_dat,
'End Date' = end_dat,
Sessions = sessions,
Pageviews = pageviews)



write.csv (ga_dat, "doesntwork.csv", row.names = TRUE)


这项工作!!!!!!!!!!!!!!!打印语句打印代码

library(googleAnalyticsR)
library(tidyverse)

#settings
start_date <- as.character(Sys.Date()-31)
end_date <- as.character(Sys.Date()-1)
metrics <- c("sessions", "pageviews")
dimensions <- "year"

#Authorize Google Analytics R- this will open a webpage
#You must be logged into your Google Analytics account on your web browser
ga_auth()

account_summary <- ga_account_list()



#Add the start and end date to the date frame, as well as some columns to use to populate the metrics
account_summary$start_date <- start_date
account_summary$end_date <- end_date


# cycle through the list of views, pull the data, and add it to the
#account_summary

for (i in 1:6){

view_id <- (Book1CSV[[1]][i])

ga_data <- google_analytics_4(viewId = view_id,
date_range = c(start_date,end_date),
metrics = metrics,
dimensions = dimensions)

# This query might return multiple rows (if it spans a year boundary), so
#collapse and clean up

ga_data <- summarise(ga_data,
sessions = sum(sessions),
pageviews = sum(pageviews))

#add the totals to the account summary

account_summary$sessions[i] <- ga_data$sessions
account_summary$pageviews[i] <- ga_data$pageviews
print(account_summary)
}

# Make a more compact set of data

clean_summary <- select(account_summary,
Account = accountName,
View = viewId,
Type = type,
Level = level,
'Start Date' = start_date,
'End Date' = end_date,
Sessions = sessions,
Pageviews = pageviews,
ID = webPropertyId)
select

write.csv (clean_summary, "worksfine.csv", row.names = FALSE)


无法真正理解这里出了什么问题。想要一些详细的帮助。我上载了一个名为 Book1CSV的文件,因为我无法获得可以捕获错误的try-catch语句。 Google商家商店的Beta版帐户导致了崩溃。

最佳答案

这是一种反R模式,可在循环中修改对象。最好返回data.frames列表,然后合并,最后创建摘要。

所以像这样:

my_view_ids <- c(12345,233445,232434)

## fetch data, create a list of
all_data <- lapply(my_view_ids, function(id){
one_data <- tryCatch(google_analytics_4(viewId = id,
date_range = c(start_date,end_date),
metrics = metrics,
dimensions = dimensions),
error = function(ex){message(ex); NULL})
one_data$viewId <- id
one_data
})

## Reduce the list of data.frames to one data.frame
all_dataframe <- Reduce(rbind, all_data)

### make your summary etc.

关于loops - Google Analytics(分析)自然搜索循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47088052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com