duanmangxie7131 2018-07-24 13:02
浏览 41

PHP:Google AnalyticsAPI页面路径过滤器不起作用,占用所有流量而不仅仅是路径

So I am crawling my blog to detect which articles have issues (broken links and linking to products we no longer sell). So I am trying to get the traffic since the article was created to a year later (unless its been done within a year, in which case it is new), then add the traffic data for that. I am doing this to prioritise the list as there are over 1000 articles with issues.

So the function of getting the anlytics was taken from Google Analytics API documentation and modified:

function getResults($analytics, $profileId, $page, $startdays, $enddays) {
return $analytics->data_ga->get(
   'ga:' . $profileId,
   $startdays.'daysAgo',
   $enddays.'daysAgo',
   'ga:sessions',
    array(
    'filters' => 'ga:pagePath=='.$page.',ga:medium==organic'));
}

function printResults($results) {
  // Parses the response from the Core Reporting API and prints
  // the profile name and total sessions.
  if (count($results->getRows()) > 0) {

    // Get the profile name.
    $profileName = $results->getProfileInfo()->getProfileName();

    // Get the entry for the first entry in the first row.
    $rows = $results->getRows();
    $sessions = $rows[0][0];

    // Print the results.
    print "Total sessions: $sessions
";
  } else {
    print "No results found.
";
  }
}

The code to actually put these parameters into the function is:

$sql="SELECT * FROM `articles_tbl` LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $url=$row["article_url"];
        $published_date=$row["date_published"]; 
        $your_date = strtotime($published_date);
        $now = time();
        $datediff=$now-$your_date;
        $startdays = round($datediff / (60 * 60 * 24));
        $enddays=$startdays-365;
        if($enddays<0){
            $enddays=1;
        }

        $analytics = initializeAnalytics();
        $profile = getFirstProfileId($analytics);

        //The domain has been removed for obvious reasons
        //this is to get the folders in the URL:
        //"/blog/news/pagefolder/"

        $page=str_replace(DOMAIN,"",$url);
        $results = getResults($analytics, $profile, $page, $startdays, $enddays);
        printResults($results);

        echo "for url: ".$page." between ".$startdays." days ago and ".$enddays." days ago.";
    }
}

So this currently outputs: "Total sessions: 999999 for url: /blog/news/pagefolder/ between 859 days ago and 494 days ago.

Now this figure above is the whole website traffic for that period (confirmed in Analytics manually), but I want the exact number of sessions for that URL only. Nothing I do will show this or change the traffic in anyway. I can only assume it gives me the sessions before the filters are applied, thus it is always the same? I have been staring at this for hours and no luck, everything I have read says that by just doing pagePath== instead of pagePath=@ should make it exact, but no suck luck, still get all website traffic...

  • 写回答

1条回答 默认 最新

  • douli0531 2018-07-25 08:10
    关注

    Turns out it was the organic filter. It was breaking the filters so none worked.

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line