Google Analaytics API Data import class for PHP.
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


GAnalytics PHP Class - Query Google Analytics API Data

A simple class to import Google Analytics data from your account.

How to:

	// Include the Google Analytics data import class
	include_once 'GAnalytics/GAnalytics.php';

	// Set up your Google Analytics credentials
	$gaEmail        = 'XXXXXX';
	$gaPassword     = 'XXXXXX';

	// Set up a period of time to get data for
	$statsStartDate = date('Y-m-d', time() - 8 * 24 * 60 *60); //one week ahead
	$statsEndDate   = date('Y-m-d', time() - 1 * 24 * 60 *60); //yesterday

	// Get and store the query data code from Google Analytics Data Feed Query Explorer
	// You will set here your own query data url
	$gaUrl = "" .
			  "ids=ga%XXXXXX&" .
			  "dimensions=ga%3ApagePath" .
			  "&metrics=ga%3Avisits&" .
			  "filters=ga%3ApagePath%3D~anunt%5C%3Fid%3D*&" .
			  "sort=-ga%3Avisits&" .
			  "start-date={$statsStartDate}&" .
			  "end-date={$statsEndDate}&" .

	// Keep your connection data into a config array
	$config = array('email'      => $gaEmail,
					'password'   => $gaPassword,
					'requestUrl' => $gaUrl,

	// Create a new GAnalytics object
	$ga = new GAnalytics($config);

	try {

		// Call the Google Analytics API request in here
		$gaResult = $ga->call();

		// If the call was successful - do your magic in here
		// You have to parse the Atom Feed XML response and gather you stats
		// This can be achieved with a SimpleXML tree traversing
		// or with a preg_match_call() to make your life easier
		preg_match_all("@<dxp:dimension name='ga:pagePath' value='/anunt\?id=([0-9]{1,})'/>@", $gaResult, $matches);

		// A dummy data rendering here...
		var_dump($matches, $matches[1], $gaResult);

	} catch (Exception $e) {

		// Log your error here
		echo "GAnalytics Connection error ({$e->getCode()}): {$e->getMessage()}";