简体   繁体   English

如何从网站的Google Analytics(分析)中查询数据?

[英]How to query data from Google Analytics of a site?

First of all I've never worked with google analytics before and now when I need to its a bit confusing to grasp the flow. 首先,我以前从未使用过Google Analytics(分析),现在,当我需要一点点混乱来掌握流程时,我从来没有使用过。

I did a lot of research online. 我在网上做了很多研究。 What I come across is that, you need to have secret key which is created at developer console to authenticate. 我遇到的是,您需要具有在开发人员控制台上创建的密钥以进行身份​​验证。 If I have this key, I can follow the standard examples found to retrieve any data I want for a site. 如果我有此密钥,则可以按照发现的标准示例检索站点所需的任何数据。

However I have a few doubts: 但是我有一些疑问:

  1. I'm working on freelance basis. 我在自由职业者的基础上工作。 So my client has given me access to their site's google analytics. 因此,我的客户使我可以访问其网站的Google Analytics(分析)。 So how to I read the analytical data like number of visitors and so on? 那么,如何读取访问人数等分析数据呢? Since my email already been allowed to access the data, I can query or do I still need the authentication key which should be in json format? 由于已经允许我的电子邮件访问数据,因此我可以查询还是仍然需要json格式的身份验证密钥?
  2. If I need to have the json key, how does it work? 如果我需要json键,它如何工作? Is it like I create a key in my developer console https://console.developers.google.com and use this key to read the client data? 就像我在开发人员控制台https://console.developers.google.com创建密钥并使用该密钥读取客户端数据一样吗? Does this key act like a one stop center to authenticate myself in accessing any api from any site as long as they have added me inside their account? 只要他们在帐户中添加了我,此密钥是否可以充当一站式中心来验证自己从任何站点访问任何api的身份?
  3. I access my client's google analytical data here: https://analytics.google.com/analytics/web 我在这里访问客户的google分析数据: https://analytics.google.com/analytics/web : https://analytics.google.com/analytics/web

Please explain to me the correct flow on how to read someone else's site data via PHP..I just need the overall idea. 请向我解释如何通过PHP读取他人站点数据的正确流程。我只需要总体思路即可。

Thank you in advance. 先感谢您。

I try with an example First of all the google client 我尝试一个例子首先,谷歌客户端

composer require "google/apiclient" 作曲家需要“ google / apiclient”

In console.developers.google.com: 在console.developers.google.com中:

  • enable analytics api 启用分析API
  • define a project (eg: project-id) 定义一个项目(例如:project-id)

2) the credentials_file 2)凭据文件

Create a service account at: 在以下位置创建服务帐户:

https://console.developers.google.com/iam-admin/serviceaccounts?project= project-id https://console.developers.google.com/iam-admin/serviceaccounts?project= project-id

在此处输入图片说明

By wich you will create the credential file at " path/to/the/service-account-credentials.json " 通过这样,您将在“ path / to / the / service-account-credentials.json ”中创建凭据文件。

{
  "type": "service_account",
  "project_id": "project-id",
  "private_key_id": "1234567890abcderf1234567890abcderf1234567890abcderf",
  "private_key": "-----BEGIN PRIVATE KEY-----\nBASE64KEY=\n-----END PRIVATE KEY-----\n",
  "client_email": "service-user@some.domain.gserviceaccount.com",
  "client_id": "000000000000000000000000000000",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/cront-reriever-search-stats%40redooc-dot-com.iam.gserviceaccount.com"
}

3) defining what you want ($infos), for witch view you want ($viewId) and a credentials file ($credentials_file) and a date range, you will query the API and got results in $response 3)定义所需的内容($ infos),对于所需的女巫视图($ viewId)和凭据文件($ credentials_file)和日期范围,您将查询API并在$ response中得到结果

 $infos= [
    'users'              => 'ga:users',
    'pageviews'              => 'ga:pageviews',
    'pageviewsPerSession' => 'ga:pageviewsPerSession',
    'unique page view'       => 'ga:uniquePageviews',
    'organicSearches'          => 'ga:organicSearches',
    'avgSessionDuration'      => 'ga:avgSessionDuration',
    'avgTimeOnPage'  => 'ga:avgTimeOnPage',
];
$credentials_file='path/to/the/service-account-credentials.json';

$viewId='1600000'; // the view ID see imgae            
$client = new \Google_Client();
$credentials_file = $this->checkServiceAccountCredentialsFile()) {
$client->setAuthConfig($credentials_file);
$client->addScope("https://www.googleapis.com/auth/analytics.readonly");
$analytics = new \Google_Service_AnalyticsReporting($client);
$response = getReport($viewId, $analytics, $infos, $DateStart, $DateEnd);

在此处输入图片说明

ADD getReport funtion 添加 getReport功能

function getReport($viewId, $analytics, $dataAnalytics, $startDate, $endDate)
    {

        $dateRange = new \Google_Service_AnalyticsReporting_DateRange();
        $dateRange->setStartDate($startDate);
        $dateRange->setEndDate($endDate);


        // Create the ReportRequest object.
        $request = new \Google_Service_AnalyticsReporting_ReportRequest();
        $request->setViewId($viewId);
        $request->setDateRanges($dateRange);

        // Create the Metrics object.
        $_metrics = [];
        foreach ($dataAnalytics as $gaLabel => $gaValue) {
            $metric = new \Google_Service_AnalyticsReporting_Metric();
            $metric->setExpression($gaValue);
//            $metric->setAlias($gaLabel);
            $_metrics[] = $metric;
        }

        $request->setMetrics($_metrics);

        $body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
        $body->setReportRequests(array($request));
        return $analytics->reports->batchGet($body);
    }

You have two options to use Site Search for POST-based search engines: 您可以使用两种方法将站点搜索用于基于POST的搜索引擎:

Option 1: Configure your web application to append the query keywords to the end of the URL (eg, http://www.example.com/search_results.php?q=keyword ) and then set up Site Search as described in the previous section. 选项1:配置您的Web应用程序以将查询关键字附加到URL的末尾(例如, http : //www.example.com/search_results.php?q=keyword ),然后如前所述设置站点搜索部分。

Option 2: Customize the tracking code on your results page to dynamically specify a virtual page path that includes the query keywords. 选项2:自定义结果页上的跟踪代码,以动态指定包含查询关键字的虚拟页路径。 The tracking code on the results page would look something like: 结果页面上的跟踪代码如下所示:

analytics.js: ga('send', 'pageview', '/search_results.php?q=keyword'); analytics.js:ga('send','pageview','/search_results.php?q=keyword');

reference: https://support.google.com/analytics/answer/1012264?hl=en 参考: https : //support.google.com/analytics/answer/1012264?hl=zh_CN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何显示Google Analytics(分析)数据对我的网站的印象/点击/ ctr? - how to show Google analytics data impressions/clicks/ctr to my site? 如何从 Google Analytics 隐藏您自己对您网站的访问? - How to Hide Your own Visits to Your Site From Google Analytics? 如何从Google Analytics(分析)自定义维度和指标中提取数据? - How to extract data from google analytics custom dimensions and metrics? 如何使用 Google 分析从分析中获取增强型电子商务数据 - How to fetch Enhanced ecommerce data from analytics using Google analytics Reporting API - PHP 使用Google Analytics API和PHP从分析帐户获取数据 - Getting data from a analytics account using Google Analytics API and PHP MVC结构化网站上的Google Analytics(分析) - google analytics on mvc structured site 如何检查Google Analytics(分析)跟踪网站的哪些部分 - How to check which sections of a site are tracked by Google Analytics 如何使用 CURL 检查网站是否有谷歌分析? - How to check site have google analytics or not using CURL? 当源中没有跟踪代码时如何判断网站是否有 Google Analytics - How to tell if a site has Google Analytics when there is no tracking code in source OAuth2,从Google Analytics(分析)中检索数据 - OAuth2, retrieve data from Google Analytics
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM