├── .gitignore ├── README.md ├── config └── ga_api.php └── libraries └── ga_api.php /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | ._* 3 | *~ 4 | nbproject 5 | *.html.gz 6 | *.js.gz 7 | *.css.gz 8 | *.xml.gz 9 | *.eot.gz 10 | *.svg.gz 11 | *.ttf.gz 12 | *.woff.gz 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #[Deprecated] Codeigniter Google Analytics Api Documentation 2 | 3 | #### Requires sessions 4 | 5 | ## Getting started 6 | 7 | 1. Download this class 8 | 2. Copy over config file into your codeigniter config folder 9 | 3. Autoload sessions and set session key in main config file 10 | 3. Copy over the library file into your library folder 11 | 4. Navigate to your new ga_api.php config file and change 12 | 13 | $config['profile_id'] // Your default Google analytics profile id 14 | (please note you can access this id from looking your Google analytics account under > settings -> profiles -> profile settings) 15 | 16 | $config['email'] = 'example@mail.com'; // GA Account mail 17 | $config['password'] = 'xxxxxxx'; // GA Account password 18 | 19 | 5. Once your config is updated, within your controller you can now. 20 | 21 | ## Example Usage 22 | 23 | $this->load->library('ga_api'); 24 | 25 | // Set new profile id if not the default id within your config document 26 | $this->ga = $this->ga_api->login()->init(array('profile_id' => '182918291281')); 27 | 28 | // Query Google metrics and dimensions 29 | // Documentation: http://code.google.com/apis/analytics/docs/gdata/dimsmets/dimsmets.html) 30 | $data = $this->ga 31 | ->dimension('adGroup , campaign , adwordsCampaignId , adwordsAdGroupId') 32 | ->metric('impressions') 33 | ->limit(30) 34 | ->get_object(); 35 | 36 | // Also please note, if you using default values you still need to init() 37 | $data = $this->ga_api->login() 38 | ->dimension('adGroup , campaign , adwordsCampaignId , adwordsAdGroupId') 39 | ->metric('impressions') 40 | ->limit(30) 41 | ->get_object(); 42 | 43 | 44 | // Please note you can also query against your account and find all the profiles associated with it by 45 | // grab all profiles within your account 46 | $data['accounts'] = $this->ga_api->login()->get_accounts(); 47 | 48 | ## Additional Metric Filters 49 | 50 | filter 51 | and_filter 52 | or_filter 53 | when 54 | sort_by 55 | limit 56 | offset 57 | segment 58 | dsegment 59 | and_dsegment 60 | or_dsegment 61 | 62 | ## Response types 63 | 64 | get 65 | get_object 66 | get_array 67 | 68 | ## Clear for cache 69 | 70 | clear // cleat init values 71 | clear_login_data // clear username & password 72 | clear_cache // clear cache folder 73 | 74 | -------------------------------------------------------------------------------- /config/ga_api.php: -------------------------------------------------------------------------------- 1 | ci =& get_instance(); 48 | $this->ci->load->config('ga_api'); 49 | 50 | $this->profile_id = $this->ci->config->item('profile_id'); 51 | $this->email = $this->ci->config->item('email'); 52 | $this->password = $this->ci->config->item('password'); 53 | 54 | $this->cache_data = $this->ci->config->item('cache_data'); 55 | $this->cache_folder = $this->ci->config->item('cache_folder'); 56 | $this->clear_cache = $this->ci->config->item('clear_cache'); 57 | 58 | $this->debug = $this->ci->config->item('debug'); 59 | } 60 | 61 | // -------------------------------------------------------------------- 62 | /** 63 | * GA_Api function. 64 | * Contructor 65 | * 66 | * @access public 67 | * @param array $config. (default: array()) 68 | * @return void 69 | */ 70 | function init($config = array()) 71 | { 72 | if (count($config) > 0) $this->initialize($config); 73 | return $this; 74 | } 75 | 76 | // -------------------------------------------------------------------- 77 | /** 78 | * Initialize the user preferences 79 | * Accepts an associative array as input, containing display preferences 80 | * 81 | * @access public 82 | * @param array config preferences 83 | * @return void 84 | */ 85 | function initialize($config = array()) 86 | { 87 | foreach ($config as $key => $val) 88 | { 89 | if (isset($this->$key)) $this->$key = $val; 90 | } 91 | if (isset($this->ci->config->clear_cache)) $this->clear_cache($this->clear_cache); 92 | } 93 | 94 | //--------------------------------------------------------------------------------------------- 95 | /** 96 | * Logs into the Google Analytics API and sets $this->auth to the authorisation token returned 97 | * 98 | * @param string $email The email address of your Google Analytics account 99 | * @param string $password Password for the account 100 | * @return boolean True if the login succeeded, false if not 101 | */ 102 | function login($email = false, $password = false) 103 | { 104 | if ($email) $this->email = $email; 105 | if ($password) $this->password = $password; 106 | 107 | $this->ci->load->library('session'); 108 | $this->auth = $this->ci->session->userdata('ga_auth'); 109 | 110 | if (! $this->auth) 111 | { 112 | $data = array( 113 | 'accountType' => 'GOOGLE', 114 | 'Email' => $this->email, 115 | 'Passwd' => $this->password, 116 | 'service' => 'analytics', 117 | 'source' => $this->source_name 118 | ); 119 | 120 | $ch = $this->curl_init($this->login_home); 121 | curl_setopt($ch, CURLOPT_POST, true); 122 | curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 123 | $output = curl_exec($ch); 124 | $info = curl_getinfo($ch); 125 | curl_close($ch); 126 | 127 | if($info['http_code'] == 200) { 128 | preg_match('/Auth=(.*)/', $output, $matches); 129 | if(isset($matches[1])) { 130 | $this->auth = $matches[1]; 131 | } 132 | } 133 | $this->ci->session->set_userdata('ga_auth', $this->auth); 134 | $this->auth = $this->ci->session->userdata('ga_auth'); 135 | } 136 | 137 | if (! $this->auth) return false; 138 | 139 | return $this; 140 | } 141 | 142 | // -------------------------------------------------------------------- 143 | /** 144 | * get_accounts function. 145 | * Récupère la liste des profils et quelques informations supplémentaires 146 | * Récupère également les segments par défaut et les segements définis par 147 | * le propriétaire du compte 148 | * 149 | * @access public 150 | */ 151 | function get_accounts($object = true) 152 | { 153 | $this->acc_file = $this->call($this->accounts_home); 154 | $dom = new DOMDocument(); 155 | $dom->loadXML($this->acc_file); 156 | 157 | //segments 158 | $segments = $dom->getElementsByTagName('segment'); 159 | foreach ($segments as $segment) 160 | { 161 | $index = $segment->getAttribute('id'); 162 | 163 | $this->accounts['segments'][$index] = $segment->getAttribute('name'); 164 | } 165 | 166 | // web-sites 167 | $entries = $dom->getElementsByTagName('entry'); 168 | foreach($entries as $entry) 169 | { 170 | $titles = $entry->getElementsByTagName('title'); 171 | $title = $titles->item(0)->nodeValue; 172 | $this->accounts[$title] = array('title' => $title); 173 | 174 | $tableIds = $entry->getElementsByTagName('tableId'); 175 | $this->accounts[$title]['tableId'] = $tableIds->item(0)->nodeValue; 176 | 177 | $properties = $entry->getElementsByTagName('property'); 178 | 179 | foreach($properties as $property) 180 | { 181 | switch($property->getAttribute('name')) 182 | { 183 | case 'ga:accountId': 184 | $this->accounts[$title]['accountId'] = $property->getAttribute('value'); 185 | break; 186 | case 'ga:accountName': 187 | $this->accounts[$title]['accountName'] = $property->getAttribute('value'); 188 | break; 189 | case 'ga:webPropertyId': 190 | $this->accounts[$title]['webPropertyId'] = $property->getAttribute('value'); 191 | break; 192 | case 'ga:profileId': 193 | $this->accounts[$title]['profileId'] = $property->getAttribute('value'); 194 | break; 195 | } 196 | } 197 | } 198 | 199 | if ($object) 200 | return (array) $this->_array_to_object($this->accounts); 201 | 202 | return $this->accounts; 203 | } 204 | 205 | // -------------------------------------------------------------------- 206 | /** 207 | * filter function. 208 | * 209 | * @access public 210 | * @param mixed $dimension_or_metric 211 | * @param mixed $filter_comparison 212 | * @param mixed $filter_value 213 | */ 214 | function filter($dim_or_met, $filter_comparison, $filter_value) 215 | { 216 | $this->filters = $this->_ga_prefix($dim_or_met) . urlencode($filter_comparison.$filter_value); 217 | 218 | return $this; 219 | } 220 | 221 | // -------------------------------------------------------------------- 222 | /** 223 | * and_filter function. 224 | * 225 | * @access public 226 | * @param mixed $dimension_or_metric 227 | * @param mixed $filter_comparison 228 | * @param mixed $filter_value 229 | */ 230 | function and_filter($dim_or_met, $filter_comparison, $filter_value) 231 | { 232 | $this->filters .= ';' . $this->_ga_prefix($dim_or_met) . urlencode($filter_comparison.$filter_value); 233 | 234 | return $this; 235 | } 236 | 237 | // -------------------------------------------------------------------- 238 | /** 239 | * or_filter function. 240 | * 241 | * @access public 242 | * @param mixed $dimension_or_metric 243 | * @param mixed $filter_comparison 244 | * @param mixed $filter_value 245 | */ 246 | function or_filter($dim_or_met, $filter_comparison, $filter_value) 247 | { 248 | $this->filters .= ',' . $this->_ga_prefix($dim_or_met) . urlencode($filter_comparison.$filter_value); 249 | 250 | return $this; 251 | } 252 | 253 | // -------------------------------------------------------------------- 254 | /** 255 | * when function. 256 | * Les temps d'entrée peuvent être soit un timestamp, soit un 257 | * string compatible avec la fonction strtotime. si on passe false 258 | * en second parametre ($end) alors cette valeur vaut la première 259 | * 260 | * @access public 261 | * @param string $start. (default: '1 month ago') 262 | * @param string $end. (default: 'yesterday') 263 | */ 264 | function when($start = '2 day ago', $end = 'yesterday') 265 | { 266 | $this->start = $this->_parse_time($start); 267 | $this->stop = $this->_parse_time($end); 268 | 269 | return $this; 270 | } 271 | 272 | // -------------------------------------------------------------------- 273 | /** 274 | * sort_by function. 275 | * On passe les paramêtres (dimension ou metric) qui doivent servir à trier les résultats. 276 | * Si la variable $inv existe, le tri se fait dans l'ordre inverse. 277 | * 278 | * @access public 279 | * @param mixed $sort 280 | */ 281 | function sort_by($arr_or_str, $inv = false) 282 | { 283 | $this->sort_by= $this->_values_converter($arr_or_str); 284 | 285 | if (! $inv) $this->sort_by= '-'.$this->sort_by; 286 | 287 | return $this; 288 | } 289 | 290 | // -------------------------------------------------------------------- 291 | /** 292 | * limit function. 293 | * Spécification du nombre de résultats et de l'index 294 | * de départ 295 | * 296 | * @access public 297 | * @param int $results. (default: 31) 298 | */ 299 | function limit($results = 31, $offset = null) 300 | { 301 | $this->max_results = $results; 302 | 303 | if ($offset) $this->offset($offset); 304 | 305 | return $this; 306 | } 307 | 308 | // -------------------------------------------------------------------- 309 | /** 310 | * offset function. 311 | * 312 | * @access public 313 | * @param int $index. (default: 10) 314 | */ 315 | function offset($index = 10) 316 | { 317 | $this->start_index = $index; 318 | return $this; 319 | } 320 | 321 | // -------------------------------------------------------------------- 322 | /** 323 | * dimension function. 324 | * 325 | * @access public 326 | * @param mixed $arr_or_str 327 | */ 328 | function dimension($arr_or_str) 329 | { 330 | $this->dimension = $this->_values_converter($arr_or_str); 331 | 332 | return $this; 333 | } 334 | 335 | // -------------------------------------------------------------------- 336 | /** 337 | * metric function. 338 | * 339 | * @access public 340 | * @param mixed $arr_or_str 341 | */ 342 | function metric($arr_or_str) 343 | { 344 | $this->metric = $this->_values_converter($arr_or_str); 345 | 346 | return $this; 347 | } 348 | 349 | // -------------------------------------------------------------------- 350 | /** 351 | * segment function. 352 | * 353 | * @access public 354 | * @param mixed $int 355 | */ 356 | function segment($int) 357 | { 358 | $this->segment = $int; 359 | 360 | return $this; 361 | } 362 | 363 | // -------------------------------------------------------------------- 364 | /** 365 | * segment function. 366 | * segment dynamique. Doit être écrit comme conformement à la syntaxe google 367 | * 368 | * @access public 369 | */ 370 | function dsegment($dim_or_met, $filter_comparison, $filter_value) 371 | { 372 | $this->dsegment = $this->_ga_prefix($dim_or_met) . urlencode($filter_comparison.$filter_value); 373 | 374 | return $this; 375 | } 376 | 377 | // -------------------------------------------------------------------- 378 | /** 379 | * and_dsegment function. 380 | * 381 | * @access public 382 | * @param mixed $dimension_or_metric 383 | * @param mixed $filter_comparison 384 | * @param mixed $filter_value 385 | */ 386 | function and_dsegment($dim_or_met, $filter_comparison, $filter_value) 387 | { 388 | $this->dsegment .= ';' . $this->_ga_prefix($dim_or_met) . urlencode($filter_comparison.$filter_value); 389 | 390 | return $this; 391 | } 392 | 393 | // -------------------------------------------------------------------- 394 | /** 395 | * or_dsegment function. 396 | * 397 | * @access public 398 | * @param mixed $dimension_or_metric 399 | * @param mixed $filter_comparison 400 | * @param mixed $filter_value 401 | */ 402 | function or_dsegment($dim_or_met, $filter_comparison, $filter_value) 403 | { 404 | $this->dsegment .= ',' . $this->_ga_prefix($dim_or_met) . urlencode($filter_comparison.$filter_value); 405 | 406 | return $this; 407 | } 408 | 409 | 410 | // -------------------------------------------------------------------- 411 | /** 412 | * get function. 413 | * La fonction permet de récupérer les infos de GA et de les afficher 414 | * directement telles quelles au format XML. 415 | * 416 | * @access public 417 | * @param Bool $file. (default: false) 418 | */ 419 | function get($file = false) 420 | { 421 | if (! $file) 422 | { 423 | $url = $this->_build_url(); 424 | 425 | if ($this->debug) 426 | { 427 | if (PHP_SAPI == 'cli') $this->ci->ouput->set_output("$url\n"); 428 | else $this->ci->output->set_output("
" . htmlentities($url) . "
\n"); 429 | } 430 | if (file_exists($this->cache_folder.md5($this->query_string).'.'.$this->cache_ext) && $this->cache_data == true) 431 | { 432 | $this->data_file = file_get_contents($this->cache_folder.md5($this->query_string).'.'.$this->cache_ext); 433 | } 434 | else 435 | { 436 | if (! $this->auth) $this->login(); 437 | 438 | $this->data_file = $this->call($url); 439 | 440 | if (! $this->data_file) return false; 441 | 442 | if ($this->cache_data == true) $this->_store_data($this->data_file); 443 | } 444 | } 445 | 446 | else if (! $this->data_file = file_get_contents($file)) return false; 447 | 448 | return $this->data_file; 449 | } 450 | 451 | // -------------------------------------------------------------------- 452 | /** 453 | * get_array function. 454 | * Retourne les données sous forme d'array 455 | * 456 | * @access public 457 | * @param mixed $config 458 | */ 459 | function get_array($file = false) 460 | { 461 | if (! $file) $xml = $this->get(); 462 | 463 | if (! $xml = $this->get($file)) return false; 464 | 465 | return $this->_xml_to_array($xml); 466 | } 467 | 468 | // -------------------------------------------------------------------- 469 | /** 470 | * get_object function. 471 | * Retourne les données sous forme d'object 472 | * 473 | * @access public 474 | * @param mixed $config 475 | */ 476 | function get_object($file = false) 477 | { 478 | if (! $file) $xml = $this->get(); 479 | 480 | if (! $xml = $this->get($file)) return false; 481 | 482 | $object = $this->_array_to_object($this->_xml_to_array($xml)); 483 | 484 | return (array) $object; 485 | } 486 | 487 | // -------------------------------------------------------------------- 488 | /** 489 | * _parse_time function. 490 | * Permet l'utilisation de certains mots clés pour les dates. 491 | * Quelque chose d'humainement lisible du style 'today', 'yesterday'...etc 492 | * On utilise simplement la fonction strtotime si on découvre un string 493 | * 494 | * @access private 495 | */ 496 | function _parse_time($time) 497 | { 498 | if (! is_numeric($time)) //on suppose que le format est compatible strtotime 499 | { 500 | if ($time === 'today') return date('Y-m-d'); 501 | else return date('Y-m-d', strtotime($time)); 502 | } 503 | else return date('Y-m-d', $time); 504 | } 505 | 506 | // -------------------------------------------------------------------- 507 | /** 508 | * _ga_prefix function. 509 | * Ajoute un préfix 'ga:'à la chaine si celui-ci n'est pas trouvé 510 | * 511 | * @access private 512 | */ 513 | function _ga_prefix($string) 514 | { 515 | if ($string[2] != ':') return 'ga:'.$string; 516 | 517 | return $string; 518 | } 519 | 520 | // -------------------------------------------------------------------- 521 | /** 522 | * _values_converter function. 523 | * traite un array ou un string (séparé par une virgule) pour ajouter 524 | * le prefix ga: avant de l'assigner à l'objet. 525 | * 526 | * @access private 527 | */ 528 | function _values_converter($arr_or_str) 529 | { 530 | if (is_string($arr_or_str) && strpos($arr_or_str, ',')) 531 | { 532 | $arr_or_str = explode(',', $arr_or_str); 533 | } 534 | 535 | if (is_array($arr_or_str)) 536 | { 537 | foreach ($arr_or_str as $key => $string) 538 | { 539 | $arr_or_str[$key] = $this->_ga_prefix(trim($string)); 540 | } 541 | $output = implode(',', $arr_or_str); 542 | } 543 | 544 | else $output = $this->_ga_prefix(trim($arr_or_str)); 545 | 546 | return $output; 547 | } 548 | 549 | // -------------------------------------------------------------------- 550 | /** 551 | * _array_to_object function. 552 | * 553 | * @access private 554 | * @param mixed $array 555 | */ 556 | function _array_to_object($array) 557 | { 558 | foreach ($array as $key => $value) 559 | { 560 | $object->$key = is_array($value) ? $this->_array_to_object($value) : $value; 561 | } 562 | return $object; 563 | } 564 | 565 | // -------------------------------------------------------------------- 566 | /** 567 | * _xml_to_array function. 568 | * La fonction permet de convertir les données du fichier XML en données 569 | * de type php 570 | * 571 | * @access private 572 | * @param mixed $xml 573 | */ 574 | function _xml_to_array($xml) 575 | { 576 | if (! $xml) return false; 577 | 578 | $this->api_error = false; 579 | $dom = new DOMDocument(); 580 | $dom->loadXML($xml); 581 | 582 | //résumé 583 | if ($this->summary === true) 584 | { 585 | $sum['accountName'] = $dom->getElementsByTagName('tableName')->item(0)->nodeValue; 586 | $sum['startDate'] = $dom->getElementsByTagName('startDate')->item(0)->nodeValue; 587 | $sum['endDate'] = $dom->getElementsByTagName('endDate')->item(0)->nodeValue; 588 | $sum['totalResults']= $dom->getElementsByTagName('totalResults')->item(0)->nodeValue; 589 | $sum['startIndex'] = $dom->getElementsByTagName('startIndex')->item(0)->nodeValue; 590 | $sum['itemsPerPage']= $dom->getElementsByTagName('itemsPerPage')->item(0)->nodeValue; 591 | if ($this->segment) $sum['segment']= $dom->getElementsByTagName('segment')->item(0)->getAttribute('name'); 592 | 593 | $aggregates = $dom->getElementsByTagName('aggregates'); 594 | 595 | foreach ($aggregates as $aggregate) 596 | { 597 | foreach($aggregate->getElementsByTagName('metric') as $metric) { 598 | $sum['metrics'][substr($metric->getAttribute('name'), 3)] = $metric->getAttribute('value'); 599 | } 600 | } 601 | $data['summary'] = $sum; 602 | } 603 | 604 | $entries = $dom->getElementsByTagName('entry'); 605 | 606 | foreach($entries as $entry) 607 | { 608 | $index = array(); 609 | foreach($entry->getElementsByTagName('dimension') as $mydimension) 610 | { 611 | $index[] = $mydimension->getAttribute('value'); 612 | } 613 | switch(count($index)) 614 | { 615 | case 0: 616 | foreach($entry->getElementsByTagName('metric') as $metric) { 617 | $data[substr($metric->getAttribute('name'), 3)] = $metric->getAttribute('value'); 618 | } 619 | break; 620 | 621 | case 1: 622 | foreach($entry->getElementsByTagName('metric') as $metric) { 623 | $data[$index[0]][substr($metric->getAttribute('name'), 3)] = $metric->getAttribute('value'); 624 | } 625 | break; 626 | 627 | case 2: 628 | foreach($entry->getElementsByTagName('metric') as $metric) { 629 | $data[$index[0]][$index[1]][substr($metric->getAttribute('name'), 3)] = $metric->getAttribute('value'); 630 | } 631 | break; 632 | 633 | case 3: 634 | foreach($entry->getElementsByTagName('metric') as $metric) { 635 | $data[$index[0]][$index[1]][$index[2]][substr($metric->getAttribute('name'), 3)] = $metric->getAttribute('value'); 636 | } 637 | break; 638 | } 639 | } 640 | return $data; 641 | } 642 | 643 | // -------------------------------------------------------------------- 644 | /** 645 | * _build_url function. 646 | * La fonction contruit le string de l'url pour appeler l'api à 647 | * partie des paramêtres passés au modèle 648 | * 649 | * @access private 650 | */ 651 | function _build_url() 652 | { 653 | if (! $this->start) $this->start = $this->_parse_time('1 month ago'); 654 | if (! $this->stop) $this->stop = $this->_parse_time('yesterday'); 655 | if (! $this->sort_by) $this->sort_by= '-'.$this->metric; 656 | 657 | $url = "ids=ga:".$this->profile_id; 658 | if ($this->dimension) $url .= "&dimensions=".$this->dimension; 659 | if ($this->metric) $url .= "&metrics=".$this->metric; 660 | if ($this->segment) $url .= "&segment=gaid::".$this->segment; 661 | else if ($this->dsegment) $url .= "&segment=dynamic::".$this->dsegment; 662 | $url .= "&sort=".$this->sort_by; 663 | $url .= "&start-date=".$this->start; 664 | $url .= "&end-date=".$this->stop; 665 | $url .= "&start-index=".$this->start_index; 666 | if ($this->max_results) $url .= "&max-results=".$this->max_results; 667 | if ($this->filters) $url .= "&filters=".$this->filters; 668 | $url.'&prettyprint='.$this->prettyprint; 669 | 670 | $this->query_string = $url; 671 | return $this->data_home.$url; 672 | } 673 | 674 | //--------------------------------------------------------------------------------------------- 675 | /** 676 | * Calls an API function using the url passed in and returns either the XML returned from the 677 | * call or false on failure 678 | * 679 | * @param string $url 680 | * @return string or boolean false 681 | */ 682 | function call($url) { 683 | 684 | $headers = array( 685 | "Authorization: GoogleLogin auth=$this->auth", 686 | "GData-Version: 2" 687 | ); 688 | 689 | $ch = $this->curl_init($url); 690 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 691 | $output = curl_exec($ch); 692 | $info = curl_getinfo($ch); 693 | curl_close($ch); 694 | 695 | // set return value to a default of false; it will be changed to the return string on success 696 | $return = false; 697 | 698 | if ($info['http_code'] == 200) $return = $output; 699 | 700 | else show_error('Google Analytics Api Library: '.$output, $info['http_code']); 701 | 702 | return $return; 703 | } 704 | 705 | //--------------------------------------------------------------------- 706 | /** 707 | * Returns an instance from curl_init with all the commonly needed properties set. 708 | * 709 | * @param $url string The $url to open 710 | */ 711 | protected function curl_init($url) { 712 | 713 | $ch = curl_init(); 714 | curl_setopt($ch, CURLOPT_URL, $url); 715 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 716 | 717 | if($this->auth) { 718 | curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: GoogleLogin auth=$this->auth")); 719 | } 720 | 721 | // Check for safe mode, CURLOPT_FOLLOWLOCATION will throw an error if safe mode is active 722 | if( ini_get('safe_mode') != FALSE ) { 723 | 724 | // the following thanks to Kyle from www.e-strategy.net 725 | // i didn't need these settings myself on a Linux box but he seemed to need them on a Windows one 726 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); 727 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 728 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 729 | } 730 | 731 | return $ch; 732 | } 733 | 734 | // -------------------------------------------------------------------- 735 | /** 736 | * _store_data function. 737 | * Sauvegarde du XML récupéré (uniqment fichier pour le moment) 738 | * 739 | * @access public 740 | */ 741 | function _store_data() 742 | { 743 | if (file_exists($this->cache_folder) && is_dir($this->cache_folder)) 744 | { 745 | if ($this->cache_data) 746 | { 747 | $file = fopen($this->cache_folder.md5($this->query_string).'.'.$this->cache_ext, 'w'); 748 | fwrite($file, $this->data_file); 749 | } 750 | return true; 751 | } 752 | return false; 753 | } 754 | 755 | // -------------------------------------------------------------------- 756 | /** 757 | * clear function. 758 | * Réinitialise les objets. Il est possible de spécifier des valeurs 759 | * à nettoyer 760 | * 761 | * @access public 762 | */ 763 | function clear($objects = false) 764 | { 765 | if (is_string($objects)) 766 | { 767 | if (strpos($objects, ',')) $objects = explode(',', $objects); 768 | else $objects = array($objects); 769 | } 770 | 771 | if (is_array($objects)) 772 | { 773 | foreach ($objects as $key) 774 | { 775 | $key = trim($key); 776 | 777 | if ($key == 'or_filter' || $key == 'and_filter') $this->filters = false; 778 | 779 | if ($key == 'or_dsegment' || $key == 'and_dsegment') $this->segment = false; 780 | 781 | else if ($key == 'when') { $this->start = ''; $this->stop = ''; } 782 | 783 | else if ($key == 'sort_by') $this->sort_by= ''; 784 | 785 | else if ($key == 'limit') $this->max_results = 31; 786 | 787 | else if ($key == 'login') $this->clear_login_data(); 788 | 789 | else if (isset($this->$key)) $this->$key = ''; 790 | } 791 | } 792 | else 793 | { 794 | $this->dimension = ''; 795 | $this->metric = ''; 796 | $this->sort_by = false; 797 | $this->start = false; 798 | $this->stop = false; 799 | $this->max_results = 31; 800 | $this->start_index = 1; 801 | $this->filters = false; 802 | $this->segdment = false; 803 | $this->clear_login_data(); 804 | } 805 | return $this; 806 | } 807 | 808 | // -------------------------------------------------------------------- 809 | /** 810 | * clear_login_data function. 811 | * Supprime les infos de connexion de la session 812 | * 813 | * @access private 814 | */ 815 | function clear_login_data() 816 | { 817 | $this->ci =& get_instance(); 818 | $this->ci->load->library('session'); 819 | 820 | $this->ci->session->unset_userdata('ga_auth'); 821 | $this->auth = false; 822 | } 823 | 824 | // -------------------------------------------------------------------- 825 | /** 826 | * clear_cache function. 827 | * Supprime les fichiers du cache 828 | * $params[0]: size ou date 829 | * $params[1]: taille total en ko, date en timestamp ou compatible strtotime 830 | * 831 | * @access public 832 | * @param array $params. (default: array()) 833 | */ 834 | function clear_cache($params = array()) 835 | { 836 | $this->ci->load->helper('file'); 837 | 838 | $cache_files = get_dir_file_info($this->cache_folder); 839 | 840 | if (! $cache_files) return false; 841 | 842 | if (! $params) $params = array('date', 9999999999999); 843 | 844 | if ($params[0] == 'size') 845 | { 846 | $total_size = 0; 847 | foreach ($cache_files as $key => $value) 848 | { 849 | $total_size = $total_size + $value['size']; 850 | } 851 | if ($total_size > $params[1]) $params[1] = 9999999999999; 852 | } 853 | 854 | else if ($params[0] == 'size' || $params[0] == 'date') 855 | { 856 | if (! is_numeric($params[1])) $params[1] = strtotime($params[1]); 857 | foreach ($cache_files as $key => $value) 858 | { 859 | if ($value['date'] < $params[1]) unlink($value['relative_path'].$key); 860 | } 861 | } 862 | 863 | else return false; 864 | 865 | return true; 866 | } 867 | } 868 | --------------------------------------------------------------------------------