douji3426 2014-02-09 17:43
浏览 29
已采纳

如何从GA API或GAPI获取某些网址的访问次数?

Basically I have a blog that I want to show the number of unique views for each post if you are logged in as an admin (I need it to calculate statistics that I can't do just looking at the regular GA admin).

So for the input it would be series of URL paths.

For example, if you're an admin looking at page 3 of the posts from a few days ago, and theres 10 posts per page, it would be 10 input paths.

Like such...

input:

'/hello-world/'
'/this-demo-post/'
'/woot/'

Then it would output something somewhat like this, where the array value is the number of unique visitors:

[0] => 1003,
[1] => 140,
[2] => 7761

At this point I would just output that in JSON on the page, and then use JS to make my calculations next to each post.

The tricky part is how to get this information from GA, as its seems to just pull in ALL of the data. Even if I limit the date to 3 days old, it still pulls all the data of way older posts that are still getting a lot of traffic. It clutters the results and may even possibly 'push out' some posts I want to see if it doesn't fit within the result scope.

I was thinking of maybe using filter regex with a lot of (this|or|that) on the 10 paths?

Here is what I have so far.

require 'gapi.class.php';

date_default_timezone_set('America/New_York');
$dimensions = array('pagePath');
$metrics    = array('uniquePageviews'); // visits
$sortMetric = null;
$filter     = null;
$startDate  = date('Y-m-d', strtotime('-2 days'));
$endDate    = date('Y-m-d');
$startIndex = 1;
$maxResults = 10000;

$ga = new gapi($gaEmail, $gaPassword);

$ga->requestReportData($profileId, $dimensions, $metrics, $sortMetric, $filter, $startDate, $endDate, $startIndex, $maxResults);

Edit In retrospect, this would all be solved with a way to sort by 'first seen' and not just by total views. Is this in GAPI?

  • 写回答

1条回答 默认 最新

  • dongxunhua2054 2014-02-10 09:23
    关注

    Tallboy,

    this one could be tricky :-)

    Let me start with the possible issues:

    • filter character limit (128 characters)
    • keep in mind that unique visitors and unique pageviews are slightly different metrics, and both could lead to strange numbers if date ranges are used incorrectly (if you run a query on daily basis for a range of days, everyone would be "unique" every single day)

    Now to answer your question:

    The tricky part is how to get this information from GA, as its seems to just pull in ALL of the data.

    The reason for this is actually that you did not specify anything in filter, so when you query your account with pagePath dimension and uniquePageviews metric, you get the numbers for all the pages.

    But would that be an issue? Could you access the row with specific page and the associated number with that page and then use it in your report?

    If not, the only reasonable and automated way to me seems splitting your query into multiple queries, and actually running it for every single page and always use the pagePath in the filter.

    If there is couple of them, that should be OK - if hundreds/thousands, not the smartest thing probably. Also, don't forget about the limits and API quota.

    What I would suggest however, is to go with custom variables / custom dimensions and send your internal PageID with every pageview. That way, the pairing could be much easier. Simply use the PostID to find the right number and display it.

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件