且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在我的网站上显示 Google Analytics 数据?

更新时间:2023-12-02 15:53:16

如果其他人遇到同样的问题,这就是我所做的,它几乎可以回答问题.

In case some one else is having the same problem here's what I did and it pretty much answers the question.

1.

以下是 API 客户端的基本代码,该客户端通过您的 Google 服务帐户访问来自 Google Analytics 的数据.https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#service_account

Here is the basic code for a API client that access data from Google Analytics via your Google Service Account. https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#service_account

为了使这个应用程序正常工作,您需要在开始编码之前准备好几件事.

In order to make this application work you need to have several things ready before you start coding.

*Google Analytics 帐户 - 注册后会生成一个跟踪器"代码,供您放置在要跟踪的每个网页上.您可能不会立即看到任何统计信息,最多可能需要 24 小时才能在 Google Analytics(分析)信息中心显示任何统计信息.

*Google Analytics Account - once registered a "tracker" code is generated for you to put on each webpage you want to track. You may not see any statistics right away and it can take up to 24h before any statistics are shown in the Google Analytics Dashboard.

带有 CLIENT_ID、CLIENT SECRET 和电子邮件地址的 OAuth 授权(API 密钥)(这不是您的普通电子邮件,而是在您进行 OAuth 授权时为您创建的服务帐户电子邮件).console.developers.google.com/

An OAuth Authorisation (API-Key) with CLIENT_ID, CLIENT SECRET and EMAIL ADRESS (This is not your normal email but a service account email that is created for you when you make an OAuth Authorisation). console.developers.google.com/

服务器密钥,也可以在此处创建:console.developers.google.com/.您还可以创建一个浏览器密钥,但没有为此烦恼,也不知道它的作用.

A serverkey, can also be created here: console.developers.google.com/. You can also create a browser key, haven't bothered with that though and don't know what it does.

最后你需要一个证书密钥.您的应用程序将只能使用密钥和凭据访问您的 Google Analytics(分析)帐户.密钥是加密的 p.12 文件.您可以在 https://code.google.com/apis/console/中找到密钥>.

Finally you need a certificate key. Your application will only be able to access your Google Analytics account by using the key and credentials. The key is an encrypted p.12 file. You can find the key in https://code.google.com/apis/console/.

这里是密钥的指南:http://www.pimcore.org/wiki/display/PIMCORE/Setup+Google+Analytics+Reporting+with+OAuth2+Service+Accounts+(自+1.4.6起)

2.

现在您拥有了所需的所有密钥和凭据,是时候开始查看我在1"中链接的代码了.这里是它的基础:https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#service_account

Now that you have all keys and credentials you need it is time to start looking at the code I linked in "1". Here is the basic for it again: https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#service_account

创建一个控制台应用程序并实现上面的代码.

Create a console application and implement the code above.

注意:您不是在制作Google Plus 服务",因此您必须为AnalyticsService"更改这些部分.转到管理 nuget 并安装软件包:

Note: your not making a "Google Plus service" so you have to change those parts for "AnalyticsService". Go to manage nuget and install packages:

  • Google API 核心库
  • Google API 客户端库
  • Google APIs 身份验证客户端库
  • Google APIs Analytics.v3 库
  • Google GData 客户端(提供用于查询数据、指标、维度等的属性)
  • Google GData 扩展库
  • 分析

可能忘记了一些东西,但这里是我使用的命名空间:

Might forgot something but here are the namespaces I use:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Security.Cryptography.X509Certificates;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Analytics.v3;

3

最后,这是我的一些代码.注意我正在创建一个新的分析,就像谷歌的代码一样应该是新的 ServiceAccountCredentials".这是主要的区别:使用 .NET 从 Google Analytics API 检索数据,多个指标?

Finally, here's is some of my code. Note I'm creating a new Analytics as supposed to "new ServiceAccountCredentials" as in the code from Google. That's the main difference: Retrieve data from Google Analytics API with .NET, multiple metrics?

有了这个,我就可以访问和查询来自 Google Analytics 帐户的数据.***的部分是您无需为此登录 Google,因为密钥和凭据可让您直接访问帐户数据.

With this I'm able to access and query data from Google Analytics account. The best part is that you don't have to log in to Google for this as the key and credentials gives you access to the account data directly.

我现在会将此代码迁移到 MVC,稍后我可能会更新如何在 Mvc 中实现此 Analytics 客户端.

I will migrate this code to MVC now I might make an update later on for how to implement this Analytics client in Mvc.