09.07
php
saved
dlh
Note
Method getReport_dlh2() intended as GA2-compatible substitute for getReport()
Method getReport_dlh2() intended as GA2-compatible substitute for getReport()
- 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.)
- 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.
- I changed the default sort column to 0, which seems to reflect reality, at least in the case of the Top Content report.
- 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).
- You'll need these defines:
- define('GA_FORMAT_CUSTOM_ARRAY', '23'); // This is a custom format introduced by this class and not part of Google Analytics
- Here are a few reports (including Unique Visitors for Chad):
- Here's the new method:
- /**
- * 9/7/07 DLH's second attempt to clone Felix's code, adapted for GA2, with original parameters preserved
- *
- * This function gets a report from Google and returns it in the desired format
- *
- * @param string $cookiePath The file path where the Cookies for this Session are stored (returned by the Login function)
- * @param number $profileId The Numeric ID of the Profile you want to get a report of (see listProfiles)
- * @param number $report Use the constants defined on top of this Document starting with GA_REPORT_... to select the report you want
- * @param mixed $fromTime Unix timestamp or strtotime() compatible format
- * @param mixed $toTime Unix timestamp or strtotime() compatible format
- * @param number $limit Maximum results to be returned; not applicable to all reports
- * @param numner $format The format you want the data to be returned. Check the GA_FORMAT... constants defined on top of this document
- * @param mixed $filter You can filter the results by giving a string to match or not match (see $filterMode)
- * @param number $filterMode Use GA_FILTER_DONT_MATCH or GA_FILTER_MATCH to control the behavior of the $filter parameter
- * @param number $sortColumn The number of the column you want to sort by, default is Column 2
- * @param number $sortOrder Use GA_SORT_ASC or GA_SORT_DESC for controlling the order your results are returned (also see $sortColumn)
- * @param mixed $cacheExpires Unix timestamp or strtotime() compatible format for the time this Result cache expires. Set to 0 for no cache
- * @return mixed Results are returned in XML, CSV or tab seperated format(see $format)
- */
- function getReport_dlh2( $cookiePath,
- $profileId,
- $report = GA_REPORT_TOP_CONTENT,
- $fromTime = '-7 days',
- $toTime = 'yesterday',
- $limit = 10,
- $format = GA_FORMAT_CSV,
- $timeline = 99, // not implemented by DLH; need usage examples
- $filter = null,
- $filterMode = GA_FILTER_MATCH,
- $sortColumn = 0,
- $sortOrder = GA_SORT_DESC,
- $cacheExpires = '+2 hours'
- ) {
- // If people skip parameters by setting them null, go back to default values
- if ( $report===null ) $report = GA_REPORT_TOP_CONTENT;
- if ( $format===null ) $format = GA_FORMAT_CSV;
- if ( $filterMode===null ) $filterMode = GA_FILTER_MATCH;
- if ( $sortOrder===null ) $sortOrder = GA_SORT_DESC;
- if ( $sortColumn===null ) $sortColumn = 0;
- if ( $format==GA_FORMAT_CUSTOM_ARRAY ) {
- $format = GA_FORMAT_XML;
- $returnArray = true;
- } else {
- $returnArray = false;
- }
- if ( ! is_numeric($fromTime) )
- $fromTime = strtotime($fromTime);
- if ( ! is_numeric($toTime) )
- $toTime = strtotime($toTime);
- $fromTime = date('Ymd', $fromTime);
- $toTime = date('Ymd', $toTime );
- $dateRange = $fromTime . '-' . $toTime;
- $url = 'https://www.google.com/analytics/reporting/export';
- 'rpt' => $report,
- 'pdr' => $dateRange,
- 'limit' => $limit,
- 'q' => $filter,
- 'qtyp' => $filterMode,
- 'tscol' => $sortColumn,
- 'tsdir' => $sortOrder,
- 'fmt' => $format,
- 'cmp' => 'average', // ???
- 'tst' => '0' // ???
- );
- // 9/7/07 should be OK to un-comment; not tested by DLH: $cachePath = $this->__createCachePath('.report.txt', $url, $vars, $cookiePath);
- // 9/7/07 should be OK to un-comment; not tested by DLH: $this->__lastCachedFile = $cachePath;
- // 9/7/07 should be OK to un-comment; not tested by DLH: $reportData = cache($cachePath, null, $cacheExpires);
- // 9/7/07 should be OK to un-comment; not tested by DLH: if (empty($reportData))
- // 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);
- return GA_ERROR_TIMEOUT;
- if ($returnArray==true) // 9/7/07 not tested by DLH!
- $reportData = $this->xmlReportToArray($reportData); // 9/7/07 not tested by DLH!
- // 9/7/07 dlh: I dump to a text file, then parse the contents and send to MySQL
- // 9/7/07 dlh: I dump to a text file, then parse the contents and send to MySQL
- // 9/7/07 dlh: I dump to a text file, then parse the contents and send to MySQL
- //
- //$sImport_filename = $this->sImport_dir . $dateRange . '_' . $profileId . '.csv';
- //
- //$this->httpGet_toFile( $sImport_filename, $url, $vars, null, $cookiePath );
- //
- //$this->scaneroo_TopContent( $sImport_filename, $dateRange, $profileId );
- //
- // 9/7/07 dlh: I dump to a text file, then parse the contents and send to MySQL
- // 9/7/07 dlh: I dump to a text file, then parse the contents and send to MySQL
- // 9/7/07 dlh: I dump to a text file, then parse the contents and send to MySQL
- return $reportData;
- }
- Hope this is useful. Feedback is welcome.
- Best-
- -David
Parsed in 0.207 seconds, using GeSHi 1.0.7.14