Skip to content Skip to sidebar Skip to footer

Google Analytics, Display More Than 1 Query

I'm pretty new to PHP so bear with me please :) Right so I am working with google analytics and I am using one of their scripts to display one query. It looks all good but I'm not

Solution 1:

It is not clear if you need to make multiple queries or if you simply need to add more metrics to your existing query. For example you can query for ga:sessions and ga:percentNewSessions in the same query.

return $analytics->data_ga->get(
    'ga:146790870',
    '2016-11-01',
    'today',
    'ga:sessions, percentNewSessions');

Then you would need to extract the second metric from the results:

  $rows = $results->getRows();
  $sessionstotal = $rows[0][0];
  $percentNewSessions = $rows[0][1];


  // Print the results.
  print "<div class='col s12 m6 l3' style='text-align:center;'>
  <div class='card green '>
        <div class='card-content white-text'>
          <span class='card-title'>Total Sessions</span>
          <p style='font-size: 1.8rem; font-weight: bold;'>$sessionstotal</p>
          <span class='card-title'>Percent New Sessions</span>
          <p style='font-size: 1.8rem; font-weight:bold;'>$percentNewSessions</p>
        </div>
        <div class='card-action  green darken-2'>
        </div>
      </div>
      </div>";

Play around with the results object until you get a sense of its structure. Always use the Reference docs to understand what fields are available.


Post a Comment for "Google Analytics, Display More Than 1 Query"