Search code examples
phpobjectgoogle-analytics-api

Instantiate multiple times same PHP object


Is it possible to instantiate multiple times the same class in a simplified way other than this I have?

include ("php/functions.php");

date_default_timezone_set('Europe/Lisbon');

require 'php/gapi.class.php';
$ga1 = new gapi('[email protected]','XXXX');
$ga2 = new gapi('[email protected]','XXXX');
$ga3 = new gapi('[email protected]','XXXX');

$ga1->requestReportData( $report_id = XXXX, $dimension = array('browser'), $metrics = array('visitors','pageviews','visits'), $sort_metric = '-visitors', $filter = 'browser == Firefox || browser == Chrome || browser == Internet Explorer || browser == Safari || browser == Opera && visits > 10', $start_date = '2012-10-17' );

$ga2->requestReportData( $report_id = XXXX, $dimension = array('country'), $metrics = array('visitors','pageviews','visits'), $sort_metric = '-visits', $filter = 'country != (not set) && visits > 3', $start_date = '2012-10-17', null, null, 10 );

$date2 = date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-15, date("Y")));
$today = date('Y-m-d');
$ga3->requestReportData($report_id = XXXX, $dimension = array('date'), $metrics = array('pageviews','visits'), $sort_metric = array('date')  , $filter = '',$date2,$today, null,15);

PS: This code works as intended ... I was just wondering if there's a more efficient way to achieve this...

EDIT: To answer some questions made on some of the solutions provided, here's an example of application a couple hundred lines later on the same file:

      <h2 class="verde">By country (10 relvant results)</h2>
      [useless code ommited...]
      <tbody>
        <?php
        $tp = 0;
        foreach($ga2->getResults() as $r2){
          if($tp==10) break;
        ?>
        <tr>
          <td><?php echo $r2 ?></td>
          <td><?php echo $r2->getVisitors() ?></td>
          <td><?php echo $r2->getVisits() ?></td>
          <td><?php echo $r2->getPageviews() ?></td>
        </tr>
        <?php
        $tp++;
        }
        ?>
        <tr style="font-weight:700">
          <td><strong>TOTAIS</strong></td>
          <td><strong><?php echo $ga2->getVisitors() ?></strong></td>
          <td><strong><?php echo $ga2->getVisits() ?></strong></td>
          <td><strong><?php echo $ga2->getPageviews() ?></strong></td>
        </tr>
      </tbody>

I'm not sure if I can post links here ... but this code can be seen on http://jb.utad.pt/gapi/


Solution

  • You just need one object to access a function. You may change the parameters passed to the function according to your needs.

    Having multiple objects of the same class, is not efficient code, as each object takes up that much extra memory.

    Hence, you just need,

     $ga = new gapi('[email protected]','XXXX');
     $ga->requestReportData( $report_id = XXXX, $dimension = array('browser'), $metrics =   array('visitors','pageviews','visits'), $sort_metric = '-visitors', $filter = 'browser == Firefox || browser == Chrome || browser == Internet Explorer || browser == Safari || browser == Opera && visits > 10', $start_date = '2012-10-17' );
     $ga->requestReportData( $report_id = XXXX, $dimension = array('country'), $metrics = array('visitors','pageviews','visits'), $sort_metric = '-visits', $filter = 'country != (not set) && visits > 3', $start_date = '2012-10-17', null, null, 10 );
     $date2 = date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-15, date("Y")));
     $today = date('Y-m-d');
     $ga->requestReportData($report_id = XXXX, $dimension = array('date'), $metrics = array('pageviews','visits'), $sort_metric = array('date')  , $filter = '',$date2,$today, null,15);