简体   繁体   English

Google Analytics API - 获取特定 URL 的页面查看信息

[英]Google Analytics API - Get page view information for specific URLs

I am looking to implement a page view tracking system on one of my websites.我希望在我的一个网站上实施页面浏览跟踪系统。

The website is a directory where suppliers can advertise their services.该网站是供应商可以宣传其服务的目录。 Suppliers have a unique profile page on the site, eg mysite.com/directory/abc-profile.html供应商在网站上有一个独特的个人资料页面,例如 mysite.com/directory/abc-profile.html

The idea is that suppliers can log in to their account area on the site and view stats on how many people are visiting their profile.这个想法是供应商可以登录到他们在网站上的帐户区域并查看有多少人访问他们的个人资料的统计数据。 Ideally I want to display this as:理想情况下,我想将其显示为:

Total views | Total today | This week | This month | This year

It does not matter if the data is not completely up to date.数据是否不是最新的也没关系。

I already have Google Analytics tracking code installed on the site.我已经在网站上安装了 Google Analytics 跟踪代码。 Is it possible to use the Google Analytics API to retrieve this data?是否可以使用 Google Analytics API 来检索这些数据? If so, what kind of query do I need to make?如果是这样,我需要进行什么样的查询? I had a look on the documentation but could not figure whether the functions allow this or not.我查看了文档,但无法确定这些功能是否允许这样做。

I am using PHP and MySQL on the server.我在服务器上使用 PHP 和 MySQL。

Yes - you will need to use the Google Analytics API for this. 是的-您需要为此使用Google Analytics(分析)API。 I would suggest checking out the Query Explorer to get a feel for the queries you will need to create. 我建议您检查一下查询浏览器,以了解您需要创建的查询。

You will require numerous queries to get all the data you need (adjusting the starting date): - Total Views - This Year - This Month - This Week (ie last 7 days - from which you could also get Total Today). 您将需要进行大量查询才能获取所需的所有数据(调整开始日期):-总观看次数-今年-本月-本周(即过去7天-您还可以从中获得今日总计)。

Here is an example query: 这是查询示例:

https://www.googleapis.com/analytics/v3/data/ga?ids=ga:1234456789&dimensions=ga:pagePath&metrics=ga:pageviews&filters=ga:pagePath==/about-us.html&start-date=2013-10-15&end-date=2013-10-29&max-results=50

Alternatively, you might want to consider www.embeddedanalytics.com ( disclosure - I work with them ). 或者,您可能要考虑www.embeddedanalytics.com公开-我与他们合作 )。 We have a service/platform that allows website owners to embed GA based charts/statistics without having to learn the GA API. 我们提供的服务/平台可让网站所有者嵌入基于GA的图表/统计信息,而无需学习GA API。 We have a CMS version which will do exactly what you need (where you script the call to pass the page path). 我们有一个CMS版本,它将完全满足您的需求(在其中编写调用以传递页面路径的脚本)。 We have done something like this with a number of podcast sharing sites. 我们在许多播客共享站点上都做了类似的事情。

Google suggests using Reporting API V4 now. Google建议立即使用Reporting API V4 The accepted answer uses V3. 接受的答案使用V3。

Here is a V4 request example: 这是V4请求示例:

POST https://analyticsreporting.googleapis.com/v4/reports:batchGet?key={YOUR_API_KEY}
{
 "reportRequests": [
  {
   "viewId": "YOUR_VIEW_ID",
   "dimensions": [
    {
     "name": "ga:pagePath"
    }
   ],
   "metrics": [
    {
     "expression": "ga:pageviews"
    }
   ],
   "dimensionFilterClauses": [
    {
     "filters": [
      {
       "operator": "EXACT",
       "dimensionName": "ga:pagePath",
       "expressions": [
        "/your-path"
       ]
      }
     ]
    }
   ],
   "dateRanges": [
    {
     "startDate": "2009-12-31",
     "endDate": "2016-09-28"
    }
   ]
  }
 ]
}

where 哪里
YOUR_API_KEY - for auth related things follow this page YOUR_API_KEY有关身份验证的内容,请关注此页面
YOUR_VIEW_ID - you can use the Account Explorer to find a View ID. YOUR_VIEW_ID您可以使用帐户资源管理器来查找数据视图ID。 (or Admin -> View -> View Settings -> View ID). (或管理员->视图->视图设置->视图ID)。

For more documentation details and a "Try it!" 有关更多文档详细信息和“请尝试!” console follow this page . 控制台遵循此页面

You should be able to add a filter on the landing page. 您应该能够在目标网页上添加过滤器。 I am assuming that each user's site has its own start page. 我假设每个用户的网站都有自己的起始页。 This returns only the data for that user. 这仅返回该用户的数据。 If you want the code on how to do this I suggest you google: Google analytics core reporting API PHP tutorial 如果您想要有关执行此操作的代码,我建议您google:Google Analytics(分析)核心报告API PHP教程

Another idea would be to let the user add there Google Analytics account to there profile. 另一个想法是让用户将那里的Google Analytics(分析)帐户添加到那里的个人资料中。 Then you can output the google analytics code onto there page. 然后,您可以将Google Analytics(分析)代码输出到该页面上。 Then they can track there own google analytics data and you won't need to deal with any of it. 然后他们可以跟踪自己的Google Analytics(分析)数据,而您无需处理任何数据。

Thought I would provide an updated version as the others are showing for V3 and V4 - for anyone using the latest API for Google Analytics Data API (GA4) .以为我会提供一个更新版本,因为其他人正在为 V3 和 V4 显示 - 对于任何使用最新 API 用于谷歌分析数据 API (GA4)的人。 This is just grabbing the total users, over a date range for a specific path.这只是在特定路径的日期范围内获取用户总数。

Note that the POST is hitting the v1 beta address as this was the latest at the time.请注意,POST 正在访问 v1 beta 地址,因为这是当时最新的。

POST https://analyticsdata.googleapis.com/v1beta/properties/PROPERTY_ID:runReport
{
  "dateRanges": [
    {
      "startDate": "YYYY-MM-DD",
      "endDate": "YYYY-MM-DD"
    }
  ],
  "dimensions": [
    {
      "name": "pagePath"
    }
  ],
  "dimensionFilter": {
    "filter": {
      "fieldName": "pagePath",
      "stringFilter": {
        "matchType": "CONTAINS",
        "value": "/YOUR/PATH"
      }
    }
  },
  "metrics": [
    {
      "name": "totalUsers"
    }
  ]
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM