2.5.4, Google Analytics uses advanced applications --API

GA is very powerful, but the free version has many limitations, such as: GA can export up to 5000 which records, if done carefully, analyze personalized undoubtedly need to use API, GA free version can take up to through the API 1 million records, paid version GA360 by the highest API is 300W, but the paid version can be associated with up BigQuery, GA will import data into account, it is possible to get all the original records.

Google official API interface to a variety of users, the overall logical structure of data FIG. 2-88 as shown below:

 

FIG 2-88 GA overall logical structure of data

 

 

Related data left is to collect data module to collect user interaction, you can see the classic version of the ga.js only directly recorded as Logs behavior, and unified version of analytics.js is used with mobile applications in a consistent manner, by measuring the agreement to phone data.

Is arranged above the intermediate can manage the processing of the data.

The underlying intermediate data processing, this is a background analysis of Google data processing, to the black box.

Reports or right provides an interface, data is processed.

Obtain authorization API principle: create a server ID GoogleAPI in the background, and then to analyze the ID granted in the account-level user management GA inside the permissions, then create OAuth 2.0 type of ID and secret key in the API, this program is on inside.

The following describes how the API to connect by GA to obtain data R and Python, R is first introduced.

1.R

GA with R to obtain the data needed RGoogle Anlytics, to download this package, with the benefits of this package is that the author has paging query functions built into it, it is possible to obtain accurate data. The setting process such as:

1, GoogleAPI management in creating a service account key,

Access Google developer background, first create a project after entering, then select the "credentials" → "Creating credentials" → "service account key", as shown 2-89:

 

Figure 2-89 Create a service account key

 

After you create will have a server account ID, as shown in Figure 2-90:

 

Figure 2-90 server account ID

 

 

2, the account ID to the server level authorization GA accounts, permission levels, is to "Reading and analysis", as shown in Figure 2-91:

 

Figure 2-91 authorized account ID to the server

 

 

3、然后返回到Google APIs管理界面,创建 OAuth 2.0授权,如图2-92所示,一般选择的应用类型是Web,如果不知道就选择其他,这个是通用型的:

 

图2-92 创建OAuthor客户授权

 

 

4、创建后会看到图2-93所示,客户端ID就是后面需要用到的了。

 

图2-93 生成秘钥

 

 

5、然后在R上,将客户端ID和密钥填入对应的位置,如图2-94所示详细的代码去GA小站官网获取。

 

图2-94 R程序提取GA获取

 

 

如果没有的包,自行下载。包RGoogleAnalytics是已经将分页查询的功能内置,所以通过这个查询出来的数据都是尽可能不抽样。上述代码中的table.id就是数据视图ID,下面试运行下如图2-95所示:

 

图2-95 R程序运行

 

 

可以看到数据已经导出来了。

2.Python

这一节介绍如何用Python连接Google Anlaytics API去获取数据,前面大部分跟R的相同,不同的地方在于Python是引入json的密钥文件,而不是直接用服务端ID和密钥。

1、假设您已经创建好了OAuth2.0客户端ID,然后将其以json格式下载,如图2-96所示:

 

图2-96 导出秘钥

 

 

可以看到有个json文件,如图2-97所示:

 

图2-97 json文件格式的秘钥

 

 

2、然后将谷歌分析官方将那段实例代码复制带本地保存,并将前面下载的json秘钥与那段代码挡在同一个路径中,然后代码找到key_file_location,将其改为json文件的名称,如图2-98所示:

 

图2-98 配置秘钥

 

 

然后试运行下看有没报错,如果没报错,会返回一些数据,那就表示成功了。

3、接下来新建另一个py文件,引入上面的示例代码,代码中的第一段就是引入该示例代码,然后初始化,这里的profile_id就是GA的数据视图ID,如图2-99所示:

 

图2-99 新建代码示例

 

 

然后运行下,如图2-100,这段代码的的意思是获取2016年7月1号开始到2016年8月10号用桌面端访问过/my/p_login.php,每天的用户数是多少,并且按日期排序:

 

图2-100 运行结果示例

 

 

数据就提取出来了。可以看到,在通过API获取的数据的过程中最关键的还是ga().get的函数配置,而配置的模式跟前面Google Sheet类似的,特别是过滤器的使用是一致。API返回的数据都是字典结构,ga().get()返回有以下数据:

3.API V4

在2017年7月的时候,GA发布了新一版的API,Version4的,授权的方式上没有改变,但是界面的UI有了变化,更重要的是查询的核心函数变成了batchGet。

下面看看设置授权的流程:

1、进入谷歌云平台控制后台创建一个项目。

2、然后单击“IAM和管理”→“账号服务”,创建一个服务账号,账号的格式会是*****@的格式,然后在GA中给这个授予“读取和分析”的权限。

3、创建和下载密钥

单击服务账号右侧的那个三个点后选择“创建密钥”,如图2-101所示:

 

图2-101 创建秘钥

 

 

然后选择p12的格式下载。

4、将官方的案例代码复制到您的编辑器去,然后更改密钥路径已经设置相关的视图ID后试运行,如图2-102所示:

 

图2-102 试运行结果

 

 

运行没有报错,获得返回的数据,API获得授权取得数据。仔细看上图的代码,这里获取数据使用的核心函数是batchGet,前面v3版本的使用的是ga().get()函数。

Guess you like

Origin www.cnblogs.com/ichdata/p/11711016.html