09.07 php saved
dlh
Tags add more
felix, getReport and google analytics  
Note
Method getReport_dlh2() intended as GA2-compatible substitute for getReport()
  1. The following code is intended as a Felix-compatible version of the getReport() method.  I implemented filters and sorting, but not the $timeline parameter.  (The $timeline parameter is included as a dummy placeholder -- if someone can give me specific usage examples I'll take a look at including it.)
  2.  
  3. As in my earlier version, I've removed caching for my own purposes.  You should be able to un-comment the relevant lines and caching will work as before.
  4.  
  5. I changed the default sort column to 0, which seems to reflect reality, at least in the case of the Top Content report.
  6.  
  7. This code is "hot off the press," which is to say it's been minimally tested.  Please bang on it and let me know what you find.  In particular, I have not tested the "xmlReportToArray" routine (and have set the default report format to CSV for safety).
  8.  
  9.  
  10. You'll need these defines:
  11.  
  12. define('GA_FORMAT_PDF', '0');
  13. define('GA_FORMAT_XML', '1');
  14. define('GA_FORMAT_CSV', '2');
  15. define('GA_FORMAT_TABSEPERATED', '3');     // for backward compatibility
  16. define('GA_FORMAT_TABSEPARATED', '3');
  17. define('GA_FORMAT_CUSTOM_ARRAY', '23');    // This is a custom format introduced by this class and not part of Google Analytics
  18.  
  19. define('GA_FILTER_MATCH', '0');       
  20. define('GA_FILTER_DONT_MATCH', '1');     
  21.  
  22. define('GA_SORT_DESC', '0');
  23. define('GA_SORT_ASC', '1');
  24.  
  25.  
  26.  
  27. Here are a few reports (including Unique Visitors for Chad):
  28.  
  29. define('GA_REPORT_TOP_CONTENT', 'TopContentReport');
  30. define('GA_REPORT_VISITORS_OVERVIEW', 'VisitorsOverviewReport');
  31. define('GA_REPORT_LANGUAGES', 'LanguagesReport');
  32. define('GA_REPORT_CONTENT_BY_TITLE', 'ContentByTitleReport');
  33. define('GA_REPORT_ABSOLUTE_UNIQUE_VISITORS', 'UniqueVisitorsReport');
  34.  
  35.  
  36.  
  37.  
  38. Here's the new method:
  39.  
  40.  
  41.   /**
  42.    * 9/7/07  DLH's second attempt to clone Felix's code, adapted for GA2, with original parameters preserved
  43.    *
  44.    * This function gets a report from Google and returns it in the desired format
  45.    *
  46.    * @param string $cookiePath        The file path where the Cookies for this Session are stored (returned by the Login function)
  47.    * @param number $profileId         The Numeric ID of the Profile you want to get a report of (see listProfiles)
  48.    * @param number $report            Use the constants defined on top of this Document starting with GA_REPORT_... to select the report you want
  49.    * @param mixed  $fromTime          Unix timestamp or strtotime() compatible format
  50.    * @param mixed  $toTime            Unix timestamp or strtotime() compatible format
  51.    * @param number $limit             Maximum results to be returned; not applicable to all reports
  52.    * @param numner $format            The format you want the data to be returned. Check the GA_FORMAT... constants defined on top of this document
  53.    * @param mixed  $filter            You can filter the results by giving a string to match or not match (see $filterMode)
  54.    * @param number $filterMode        Use GA_FILTER_DONT_MATCH or GA_FILTER_MATCH to control the behavior of the $filter parameter
  55.    * @param number $sortColumn        The number of the column you want to sort by, default is Column 2
  56.    * @param number $sortOrder         Use GA_SORT_ASC or GA_SORT_DESC for controlling the order your results are returned (also see $sortColumn)
  57.    * @param mixed  $cacheExpires      Unix timestamp or strtotime() compatible format for the time this Result cache expires. Set to 0 for no cache
  58.    * @return mixed                    Results are returned in XML, CSV or tab seperated format(see $format)
  59.    */
  60.  
  61.   function getReport_dlh2( $cookiePath,
  62.                            $profileId,
  63.                            $report       = GA_REPORT_TOP_CONTENT,
  64.                            $fromTime     = '-7 days',
  65.                            $toTime       = 'yesterday',
  66.                            $limit        = 10,
  67.                            $format       = GA_FORMAT_CSV,
  68.                            $timeline     = 99,                          // not implemented by DLH; need usage examples
  69.                            $filter       = null,
  70.                            $filterMode   = GA_FILTER_MATCH,
  71.                            $sortColumn   = 0,
  72.                            $sortOrder    = GA_SORT_DESC,
  73.                            $cacheExpires = '+2 hours'
  74.                         ) {   
  75.    
  76.                          
  77.     // If people skip parameters by setting them null, go back to default values
  78.     if ( $report===null )     $report     = GA_REPORT_TOP_CONTENT;
  79.     if ( $format===null )     $format     = GA_FORMAT_CSV;
  80.     if ( $filterMode===null ) $filterMode = GA_FILTER_MATCH;
  81.     if ( $sortOrder===null )  $sortOrder  = GA_SORT_DESC;
  82.     if ( $sortColumn===null ) $sortColumn = 0;
  83.    
  84.        
  85.     if ( $format==GA_FORMAT_CUSTOM_ARRAY ) {
  86.          
  87.       $format = GA_FORMAT_XML;
  88.       $returnArray = true;
  89.            
  90.     } else {
  91.            
  92.       $returnArray = false;
  93.            
  94.     }
  95.  
  96.    
  97.     if ( ! is_numeric($fromTime) )
  98.       $fromTime = strtotime($fromTime);
  99.  
  100.     if ( ! is_numeric($toTime) )
  101.       $toTime = strtotime($toTime);     
  102.    
  103.     $fromTime = date('Ymd', $fromTime);
  104.     $toTime   = date('Ymd', $toTime  );
  105.    
  106.     $dateRange = $fromTime . '-' . $toTime;
  107.    
  108.      
  109.     $url = 'https://www.google.com/analytics/reporting/export';
  110.        
  111.  
  112.        
  113.     $vars = array( 'id'    => $profileId,
  114.                    'rpt'   => $report,
  115.                    'pdr'   => $dateRange,
  116.                    'limit' => $limit,
  117.                    'q'     => $filter,
  118.                    'qtyp'  => $filterMode,
  119.                    'tscol' => $sortColumn,
  120.                    'tsdir' => $sortOrder,
  121.                    'fmt'   => $format,                 
  122.                    'cmp'   => 'average',       // ???
  123.                    'tst'   => '0'              // ???
  124.                    );
  125.  
  126.  
  127.            
  128.     // 9/7/07 should be OK to un-comment; not tested by DLH:  $cachePath = $this->__createCachePath('.report.txt', $url, $vars, $cookiePath);
  129.     // 9/7/07 should be OK to un-comment; not tested by DLH:  $this->__lastCachedFile = $cachePath;
  130.        
  131.     // 9/7/07 should be OK to un-comment; not tested by DLH:  $reportData = cache($cachePath, null, $cacheExpires);
  132.        
  133.     // 9/7/07 should be OK to un-comment; not tested by DLH:  if (empty($reportData))                       
  134.     // 9/7/07 should be OK to un-comment; not tested by DLH:  $reportData = cache($cachePath, utf8_encode($this->httpGet($url, $vars, null, $cookiePath)), $cacheExpires);
  135.  
  136.    
  137.     $reportData = utf8_encode( $this->httpGet($url, $vars, null, $cookiePath) );
  138.            
  139.     if (empty($reportData))       // No Data - Probably a Connection timeout
  140.       return GA_ERROR_TIMEOUT;
  141.                          
  142.                          
  143.                          
  144.            
  145.     if ($returnArray==true)                                     // 9/7/07 not tested by DLH!                   
  146.       $reportData = $this->xmlReportToArray($reportData);       // 9/7/07 not tested by DLH!   
  147.  
  148.  
  149.    
  150.     // 9/7/07 dlh: I dump to a text file, then parse the contents and send to MySQL 
  151.     // 9/7/07 dlh: I dump to a text file, then parse the contents and send to MySQL 
  152.     // 9/7/07 dlh: I dump to a text file, then parse the contents and send to MySQL
  153.     // 
  154.     //$sImport_filename = $this->sImport_dir . $dateRange . '_' . $profileId . '.csv';     
  155.     //
  156.     //$this->httpGet_toFile( $sImport_filename, $url, $vars, null, $cookiePath );
  157.     //   
  158.     //$this->scaneroo_TopContent( $sImport_filename, $dateRange, $profileId );
  159.     //
  160.     // 9/7/07 dlh: I dump to a text file, then parse the contents and send to MySQL 
  161.     // 9/7/07 dlh: I dump to a text file, then parse the contents and send to MySQL 
  162.     // 9/7/07 dlh: I dump to a text file, then parse the contents and send to MySQL 
  163.    
  164.    
  165.      
  166.     return $reportData;
  167.    
  168.        
  169.   }
  170.  
  171.  
  172.  
  173.  
  174.  
  175. Hope this is useful.  Feedback is welcome.
  176.  
  177. Best-
  178. -David
  179.  
Parsed in 0.207 seconds, using GeSHi 1.0.7.14

Modify this Paste