My twitter
- Ce mois-ci, record de visites pour le blog. Belle perf pour un mois d'août ! 06:52:00 août 31, 2010 from Facebook
- Record de visites ce mois-ci sur willdurand.fr. Je ne dois pas raconter que des bétises... 06:45:29 août 31, 2010 from TweetDeck
- @Ouark y'a une technique de maître/esclave au niveau des domaines sinon... 05:37:57 août 31, 2010 from TweetDeckin reply to Ouark
- @Ouark ok j'avais mal compris. C'est donc pour le tracking, l'image est une bonne solution, sauce analytics. :) 05:01:50 août 31, 2010 from TweetDeckin reply to Ouark
- @oloynet @Ouark modif sessionstorage dans factories.yml + memcached ou database non ? 04:39:55 août 31, 2010 from TweetDeckin reply to oloynet
- http://www.voyages-sncf.com/plusloinquevousnelimaginez/ 02:31:18 août 31, 2010 from TweetDeck
- @chessman2212 @mazenovi oki 19h 12:14:52 août 30, 2010 from TweetDeckin reply to chessman2212
- @vjousse thank you, i'll do that. 12:01:30 août 30, 2010 from TweetDeckin reply to vjousse
- How can I become sponsor of cmf.symfony-project.org ? #Symfony2 10:06:28 août 30, 2010 from TweetDeck
- @mazenovi @gidehault @chessman2212 @ybb_fr idem dispo. 10:04:13 août 30, 2010 from TweetDeckin reply to mazenovi
RSS Feed
Liens
API Google Analytics – Une classe PHP5 et des résultats d’intégration
{
private $email;
private $passwd;
private $ids;
private $auth;
private static $instance;
private function __construct()
{
$this->login('********@********', '********', '********');
}
private function __clone()
{}
public static function instance()
{
if(!isset(self::$instance))
{
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}
private function login($email, $passwd, $ids)
{
$this->email = $email;
$this->passwd = $passwd;
$this->ids = $ids;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = array('accountType' => 'GOOGLE',
'Email' => $this->email,
'Passwd' => $this->passwd,
'source'=>'CLI_GAnalytics',
'service'=>'analytics');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$hasil = curl_exec($ch);
$hasil = @split("Auth=", $hasil);
curl_close($ch);
$this->auth = $hasil[1];
}
public function getDimensionByMetric($metrics, $dimensions, $date_1, $date_2 = null)
{
if(!$date_2)
$date_2 = $date_1;
$ch = curl_init("https://www.google.com/analytics/feeds/data?ids=ga:" . $this->ids . "&metrics=ga:" . $metrics . "&dimensions=ga:" . $dimensions . "&start-date=" . $date_1 . "&end-date=" . $date_2);
$header[] = 'Authorization: GoogleLogin auth=' . $this->auth;
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$response = curl_exec($ch);
$infos = curl_getinfo($ch);
curl_close($ch);
if($infos['http_code'] != 200)
throw new Exception("[EXCEPTION] (" . $info['http_code'] . ") " . $response);
$XML_response = @str_replace('dxp:','',$response);
$XML_object = simplexml_load_string($XML_response);
$data = '';
$label = '';
foreach($XML_object->entry as $m)
{
$tmp = @split('ga:' . $dimensions . '=', $m->title);
if($label == "")
{
$label .= $tmp[1] . ' (' . $m->metric['value'] . ')';
$data .= $m->metric['value'];
}
else
{
$label .= '|' . $tmp[1] . ' (' . $m->metric['value'] . ')';
$data .= ',' . $m->metric['value'];
}
}
return array('label' => $label, 'data' => $data);
}
public function getMetric($metric, $date_1, $date_2 = null)
{
if(!$date_2)
$date_2 = $date_1;
$ch = curl_init("https://www.google.com/analytics/feeds/data?ids=ga:" . $this->ids . "&metrics=ga:" . $metric . "&start-date=" . $date_1 . "&end-date=" . $date_2);
$header[] = 'Authorization: GoogleLogin auth=' . $this->auth;
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$response = curl_exec($ch);
$infos = curl_getinfo($ch);
curl_close($ch);
if($infos['http_code'] != 200)
throw new Exception("[EXCEPTION] (" . $info['http_code'] . ") " . $response);
$XML_response = @str_replace('dxp:','',$response);
$XML_object = simplexml_load_string($XML_response);
return $XML_object->entry->metric['value'] ? $XML_object->entry->metric['value'] : 0;
}
public function getMetricURI($metric, $uri, $date_1, $date_2 = null)
{
if(!$date_2)
$date_2 = $date_1;
$ch = curl_init("https://www.google.com/analytics/feeds/data?ids=ga:" . $this->ids . "&metrics=ga:" . $metric . "&dimensions=ga:pagePath&filters=ga:pagePath=" . $uri . "&start-date=" . $date_1 . "&end-date=" . $date_2);
$header[] = 'Authorization: GoogleLogin auth=' . $this->auth;
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$response = curl_exec($ch);
$infos = curl_getinfo($ch);
curl_close($ch);
if($infos['http_code'] != 200)
throw new Exception("[EXCEPTION] (" . $info['http_code'] . ") " . $response);
$XML_response = @str_replace('dxp:','',$response);
$XML_object = simplexml_load_string($XML_response);
return $XML_object->entry->metric['value'] ? $XML_object->entry->metric['value'] : 0;
}
}
?>
Pour récupérer une instance de cette classe on appelle la méthode statique instance() :
Ensuite on peut utiliser les méthodes comme dans l’exemple ci-dessous :
$countries = $ga->getDimensionByMetric('visits', 'country', date('Y-m-d', time()));
$visits = $ga->getMetric('visits', date('Y-m-d', time()));
$unique_visits = $ga->getMetric('visitors', date('Y-m-d', time()));
$page_views = $ga->getMetric('pageviews', date('Y-m-d', time()));
On passe soit un intervalle (une période), soit une seule date.
Cette classe et ces méthodes sont très perfectibles, mais elles me suffisent amplement pour une intégration basique de Google Analytics. Ceci m’a permis d’avoir le résultat suivant :
Liens annexes :
http://www.willdurand.fr/api-google-analytics-decouverte-par-l-exemple/
http://www.willdurand.fr/api-google-analytics-round-2-graphiques-et-statistiques-par-page/
Related Posts