google 数据分析实习_谷歌分析数据与R

google 数据分析实习

Objective: To programmatically retrieve Google Analytics data for marketing analytics automation.

目标:以编程方式检索Google Analytics(分析)数据以进行营销分析自动化。

Accessing Google Analytics API to retrieve GA records is one of the quintessential requirements to build an end-to-end marketing analytics suite. We could achieve this objective through four major steps as listed here below:

访问Google Analytics(分析)API以检索GA记录是构建端到端营销分析套件的基本要求之一。 我们可以通过以下四个主要步骤来实现此目标:

  1. Generate Client ID and Secret Key in Google Cloud.

    在Google Cloud中生成客户端ID和密钥。
  2. Update .Renviron variables.

    更新.Renviron变量。
  3. Import relevant libraries and refresh GA tokens locally.

    导入相关库并在本地刷新GA令牌。
  4. Finally, build the GA dataset in R.

    最后,在R中建立GA资料集。

Step 1. Generate Client ID and Secret Key in Google Cloud

步骤1.在Google Cloud中生成客户端ID和密钥

Step 1.1. Create a Google Cloud Project: Sign in to Google Cloud Console and create a project.

步骤1.1。 创建Google Cloud项目:登录Google Cloud Console并创建一个项目。

Image for post
Create a Google Cloud Project (Image by Author)
创建一个Google Cloud Project(作者提供的图片)

Step 1.2. Google Analytics Reporting API: Once when you have created the project, navigate to the project’s API’s and Services section, and enable ‘Google Analytics Reporting API’.

步骤1.2。 Google Analytics Reporting API:创建项目后,导航至项目的API和“服务”部分,然后启用“ Google Analytics Reporting API”。

Image for post
Google Analytics Reporting API 1 (Image by Author)
Google Analytics Reporting API 1(作者提供)
Image for post
Google Analytics Reporting API 2 (Image by Author)
Google Analytics Reporting API 2(作者提供)
Image for post
Google Analytics Reporting API 3 (Image by Author)
Google Analytics Reporting API 3(作者提供)

Step 1.3. Configure OAuth Consent Screen: If you are setting up Google Cloud Project for the first time, you will have to configure the OAuth Consent Screen before generating the credentials. Ensure that you choose the Analytics Reporting APIs in the project scope post entering the application name and support email in the consent screen.

步骤1.3。 配置OAuth同意屏幕:如果是首次设置Google Cloud Project,则必须先配置OAuth同意屏幕,然后才能生成凭据。 确保在项目范围后选择“ Analytics Reporting API”,然后在“同意”屏幕中输入应用程序名称和支持电子邮件。

Image for post
Configure OAuth Consent Screen 1 (Image by Author)
配置OAuth同意屏幕1(作者提供的图像)
Image for post
Configure OAuth Consent Screen 2 (Image by Author)
配置OAuth同意屏幕2(作者提供的图像)
Image for post
Configure OAuth Consent Screen 3 (Image by Author)
配置OAuth同意屏幕3(作者提供的图像)

Step 1.4. Create OAuth Client ID: Post configuring the OAuth consent screen, create OAuth Client ID credentials. Download the Client ID and Secret Key as a JSON file and store it in your current working directory.

步骤1.4。 创建OAuth客户端ID:在配置OAuth同意屏幕后,创建OAuth客户端ID凭据。 将客户端ID和密钥下载为JSON文件,并将其存储在当前工作目录中。

Image for post
Create OAuth Client ID 1 (Image by Author)
创建OAuth客户端ID 1(作者提供的图像)
Image for post
Create OAuth Client ID 2 (Image by Author)
创建OAuth客户端ID 2(作者提供的图像)
Image for post
Create OAuth Client ID 3 (Image by Author)
创建OAuth客户端ID 3(作者提供的图像)

Step 2. Update .Renviron: Edit .Renviron by integrating the latest google cloud project credentials

第2步。更新.Renviron:通过集成最新的Google Cloud Project凭据来编辑.Renviron

Step 2.1. Open .Renviron using the following command in R and update the parameters:

步骤2.1。 在R中使用以下命令打开.Renviron并更新参数:

usethis:: edit_r_environ()
Image for post
(Image by Author) (图片由作者提供)

Step 3. GA Authentication: Start an R session and import all the relevant libraries upfront

步骤3. GA身份验证:启动R会话并预先导入所有相关库

googleAnalyticsR and googleAuthR are the must-have’s in the list. Ensure that you use ‘gar_set_client ()’ function before loading the googleAnalyticsR and googleAuthR packages. Also, point the location of the ‘refresh token’ inside the gar_auth() function.

googleAnalyticsR和googleAuthR是列表中的必备项。 在加载googleAnalyticsR和googleAuthR软件包之前,请确保使用“ gar_set_client()”函数。 另外,在gar_auth()函数中指向“刷新令牌”的位置。

googleAuthR:: gar_set_client( "C:\\Users\\Sree\\gcp_client.json")library(googleAnalyticsR)
library(googleAuthR)
library(tidyverse)
library(lubridate)
library(dplyr)googleAuthR::gar_auth(token = "sc_ga.httr-oauth")

Upon executing the aforementioned command, you would be able to successfully log into the Google Analytics Reporting Database via R through automated refresh tokens.

执行上述命令后,您将能够通过R通过自动刷新令牌成功登录Google Analytics(分析)报告数据库。

Step 4. Google Analytics Reporting API: Build the GA report in R

第4步。GoogleAnalytics Reporting API:在R中生成GA报告

Step 4.1. Get the list of views from GA:

步骤4.1。 从GA获取视图列表:

Identify the entire list of views that you need from Google Analytics. Get their ViewId’s either by using the ‘ga_account_list()’ function in googleAuthR package or by identifying them from the Google Analytics itself.

确定您需要从Google Analytics(分析)中获取的全部视图。 通过使用googleAuthR软件包中的“ ga_account_list()”函数或从Google Analytics(分析)本身中识别出他们的ViewId。

Image for post
Google Analytics Reporting API (Image by Author)
Google Analytics Reporting API(作者提供的图像)
my_accounts <- ga_account_list()
viewId <- (my_accounts$viewId)

Step 4.2. Build the dataset by querying Google Analytics Reporting Server:

步骤4.2。 通过查询Google Analytics(分析)Reporting Server构建数据集:

For the purpose of illustration, here I have built a loop that downloads data sequentially for all the views that have been set up in Google Analytics.

出于说明目的,我在这里建立了一个循环,该循环为Google Analytics(分析)中设置的所有视图顺序下载数据。

ga_data_final <- data.frame()for (i in viewId) {
ga_data_temp <-
google_analytics(i,
date_range = c(GoogleA_Start, GoogleA_End),
metrics = c("sessions"),
dimensions = c("date"
,"channelGrouping"
,"deviceCategory"
,"source"),
anti_sample = TRUE,
#slow_fetch = TRUE,
max = c("-1"))
ga_data_temp$viewId <- i
ga_data_final <- rbind(ga_data_final, ga_data_temp)
}

Step 4.3. Clean the dataset:

步骤4.3。 清理数据集:

GA_export <- 
left_join(ga_data_final,my_accounts, by = "viewId") %>%
select(date,channelGrouping,deviceCategory,source,sessions,Country) %>%
mutate(channelGrouping_refined = if_else(
channelGrouping == 'Organic Search','SEO',
if_else(channelGrouping == 'Paid Search','PPC Search',
if_else(channelGrouping == 'Display', 'PPC Display',
if_else(channelGrouping == 'email,email','EDM',
if_else(channelGrouping == '(Other)', 'Unspecified', channelGrouping
)))))) %>% select(date,channelGrouping,deviceCategory,source
,sessions,channelGrouping_refined,Country)rm(my_accounts,ga_data_final,ga_data_temp,i, viewId)

Step 4.4. Publish the dataset:

步骤4.4。 发布数据集:

Here’s a snapshot of the final output from the Google Analytics Reporting server.

这是Google Analytics(分析)报告服务器最终输出的快照。

Image for post
Publish the Dataset (Image by Author)
发布数据集(作者提供的图像)

Finally, publish the dataset with a timestamp either into a flat-file or into a cloud database.

最后,将带有时间戳的数据集发布到平面文件或云数据库中。

GA_data <- 
GA_export %>%
write_csv("GA_data.csv")

Step 5: What’s Next?

步骤5:下一步是什么?

This is just the inception of marketing analytics automation. As we could do so much more on top of this like:

这仅仅是营销分析自动化的开始。 我们可以在此基础上做更多的事情,例如:

1) Automate the R scripts on a virtual machine and store the incremental data in a cloud database.

1)在虚拟机上自动化R脚本,并将增量数据存储在云数据库中。

2) Repeat the same set of processes to build an automated cross channel data lake that brings in incremental Facebook Ads /Google Ads /Twitter Ads datasets.

2)重复相同的过程集,以构建一个自动的跨渠道数据湖,以引入增量Facebook Ads / Google Ads / Twitter Ads数据集。

3) Forecast the user sessions/metrics from each platform using time series forecasting.

3)使用时间序列预测来预测每个平台的用户会话/指标。

4) Lastly, build a BI dashboard that retrieves data from the flat file/ cloud storage to visualize an end-to-end marketing analytics suite.

4)最后,构建一个BI仪表板,该仪表板从平面文件/云存储中检索数据,以可视化端到端营销分析套件。

About the Author

关于作者

Feel free to reach out to me if you need any help in understanding the fundamentals of GA automation in R. Hope this helps:)

如果您需要任何帮助来了解R中的GA自动化基础知识,请随时与我联系。希望这会有所帮助:)

翻译自: https://towardsdatascience.com/google-analytics-data-with-r-32caf8956c94

google 数据分析实习

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值