开发者

Google analytics api. To sort result while in the loop

开发者 https://www.devze.com 2023-03-13 11:40 出处:网络
i need help with google analytics gapi class with php. (http://code.google.com/p/gapi-google-analytics-php-interface)

i need help with google analytics gapi class with php. (http://code.google.com/p/gapi-google-analytics-php-interface)

I want to output how many times each item in catalog was viewed. The page for the item generates with id for example:

  1. /item.php?id=1
  2. /item.php?id开发者_如何学运维=2
  3. ect.

So everything goes right with my code until i want to order by the most viewed item, since i am using loop, to generate random filters: $filter = "pagePath == /item.php?id=".$i++."";

I am not able to use sort in gapi "requestReportData".

With the code shown below everyting outputs right, but i don't know how to sort everything so it will shown results from the most viewed item till least.

The code:

$ga = new gapi(ga_email,ga_password);
$dimensions = array('pagePath');
$metrics   = array('pageviews');    
$termFrom = 2011-06-01;
$termUntil = 2011-06-30;
echo '<strong>ITEMS VIEW STATISTIC</strong><br />';
 for ( $i='1'; $i<'20';)
    {
      $filter = "pagePath == /item.php?id=".$i++."";
      $ga->requestReportData(table_id,$dimensions,$metrics,'-pageviews',$filter,   $termFrom, $termUntil);
        foreach($ga->getResults() as $result)
           { $j= $i-1; $b=$j-1;
              echo $z++.') Items which ID is:'.$j++.' and NAME is: '.$ItemsNamesArray[$b]['item_name'].' was viewed: '.$result->getpageviews() . ' times<br />';
            }
      }

It outputs:

ITEMS VIEW STATISTIC

  • 1) Items which ID is:1 and NAME is: Book was viewed: 9 times
  • 2) Items which ID is:2 and NAME is: Box: 1 times
  • 3) Items which ID is:3 and NAME is: Table: 3 times
  • 4) Items which ID is:4 and NAME is: House: 27 times

I want it to output:

ITEMS VIEW STATISTIC

  • 1) Items which ID is:4 and NAME is: House was viewed: 27 times
  • 2) Items which ID is:1 and NAME is: Book was viewed: 9 times
  • 3) Items which ID is:3 and NAME is: Table was viewed: 3 times
  • 4) Items which ID is:2 and NAME is: Box was viewed: 1 times


You can use regular expressions for filters to get all your twenty items at once and have Google Analytics sort them:

$ga             = new gapi(ga_email,ga_password);

$dimensions     = array('pagePath');
$metrics        = array('pageviews');    
$termFrom       = '2011-06-01';
$termUntil      = '2011-06-30';
$filter         = 'pagePath=~/item\.php\?id=[0-9]*' // Matches all item URLs
$sort           = '-pageviews'; // Sorted by desc. pageview count
$maxResults     = 20; // First 20 entries

$ga->requestReportData(table_id, $dimensions, $metrics, $sort, $filter, $termFrom, $termUntil, 1, $maxResults);

foreach($ga->getResults as $i => $result){
    // Do your magic for each item
}

This is untested, the regular expression in the filter should match correctly, though.

I assumed you want the twenty most-viewed item URLs.

0

精彩评论

暂无评论...
验证码 换一张
取 消