Display Google Analytics data on my web site?

前端 未结 6 928
终归单人心
终归单人心 2021-01-31 17:35

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 vi

6条回答
  •  天命终不由人
    2021-01-31 18:16

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

    1.

    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 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.

    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/

    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.

    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/.

    Here is a guide for the key: http://www.pimcore.org/wiki/display/PIMCORE/Setup+Google+Analytics+Reporting+with+OAuth2+Service+Accounts+(since+1.4.6)

    2.

    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.

    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 Apis Core Library
    • Google Apis Client Library
    • Google Apis Auth Client Library
    • Google Apis Analytics.v3 Library
    • Google GData Client (This provides properties used to query data, metrics, demensions etc)
    • Google GData Extensions Library
    • Analytics

    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

    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?

    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.

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

提交回复
热议问题