在我的网站上显示 Google Analytics 数据? [英] Display Google Analytics data on my web site?

查看:25
本文介绍了在我的网站上显示 Google Analytics 数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力想办法在我的网站上显示从 Google Analytics 收集的数据.我正在使用 NopCommerce,我想在管理部分的视图中显示此信息/统计信息.

I'm trying to figure out a way to display data collected from Google Analytics on my web site. I'm using NopCommerce, and I want to display this information/statistics in a view in the Admin Section.

可能有很多方法可以实现这一点,在网上搜索后我找到了一些使用 JavaScript 的例子,但我找不到一个很好的教程.

There might be many ways to achieve this, and after searching the web I found some examples using JavaScript, but I couldn't find a good tutorial for this.

我还研究了将 Google Analytics 与 C# 集成,我发现了这个例子:http://biasecurities.com/2012/02/using-the-google-analytics-api-with-asp-net-mvc/#comment-1310可以从 GitHub 下载演示项目:https://github.com/jgeurts/Analytics-Example

I have also looked into integrating Google Analytics with C#, and I found this example: http://biasecurities.com/2012/02/using-the-google-analytics-api-with-asp-net-mvc/#comment-1310 A demo project can be downloaded from GitHub here: https://github.com/jgeurts/Analytics-Example

但是,演示项目似乎无法用作 google URL (https://www.google.com/analytics/feeds/accounts/default) 不再使用.

However the demo project doesn't seem to work as the google URL (https://www.google.com/analytics/feeds/accounts/default) is no longer in use.

当我使用 MVC 应用程序时,最好通过在控制器中应用 Google Analytics 逻辑并将其显示在视图中来实现这一点.或者类似的东西.

As I'm using a MVC application it would be preferable to make this happen by applying the Google Analytics logic within a Controller and displaying it in a view. Or something like that.

谷歌在这里提供了一个查询工具来试验,所以它不应该是从谷歌分析中硬提取数据并在网站上显示数据:https://ga-dev-tools.appspot.com/explorer/

Google provides a query tool to experiment with here, so it shouldn't be to hard extracting data from Google Analytics and display the data on the website: https://ga-dev-tools.appspot.com/explorer/

有没有人能够在他们的网站上成功显示 Google Analytics 数据?

Has anyone been able to successfully display Google Analytics data on their website?

推荐答案

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

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.

这篇关于在我的网站上显示 Google Analytics 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆