Laravel

Display Google Analytics Real time Users In Laravel 5.6

March 5, 2018

author:

Display Google Analytics Real time Users In Laravel 5.6

At times, Web application needs a feature to display current active users (i.e., real-time users) in a web page. There are various ways to attain this, but one of the most efficient and trusted ways is through Google Analytics. Today, we will integrate one such use case in a Laravel Application.

# Laravel Google Analytics Setup

But to do so, we need various setups. We need to install a third party package from spatie in our Laravel app. We all require to create a service account in Google Developers’ Console and add the same in Google Analytics Account as a way to permit our application to read Analytics data. We covered all these in the last post where we started from scratch and worked step – by – step to successfully complete this. If you haven’t, go through the Google Analytics Set up In Laravel first and then come back to this one.

# Real-time Data

Once we have basic set up ready, we can dive into some real stuff. In this post we will work with two work cases:
• View Single Application Data
• View Multiple Application Data

# Display Real-time Users for Single Application

This can be achieved quite easily. We can do so with spatie/laravel-analytics package by accessing Google_Service_Analytics object.

public function getActiveUsers()
{
    $live_users = Analytics::getAnalyticsService()->data_realtime->get('ga:'.env('ANALYTICS_VIEW_ID'), 'rt:activeVisitors')->totalsForAllResults['rt:activeVisitors'];
    return view('analytics',compact('live_users'));	
}

Here, the .env file must store the View ID we fetched in the last post.

To display real-time users in Laravel, we can use blade view. To render data from the controller:

<div class="project-stats">
	{{$live_users}}
</div>

# Display Real-time Users for Multiple Application

It can get tricky when you want your Laravel Application to fetch data for more than one View ID with this package. We need to tweak it a little. Firstly, to get more View IDs, go to your Google Analytics account and follow the same steps as we did in the last post. And then in Laravel Controller:

public function getActiveUsers()
{
    $view_ids['app1'] = 199978646 
    $view_ids['app2'] = 179218644 
    $live_users = [];
    foreach($view_ids as $key => $id){
        $live = Analytics::getAnalyticsService()->data_realtime->get('ga:'.$id, 'rt:activeVisitors')->totalsForAllResults['rt:activeVisitors'];
        $live_data[$key] = $live;
    }
                
    return view('analytics',compact('live_users'));			
}

Note, for simplicity; we are storing the View IDs in the controller itself. For production apps, you may want to move them in the .env file to keep them confidential.

Now, to render data from multiple applications we created above, you can iterate through the result:

<div class="table-responsive project-stats">
  <ul>
    @foreach($live_users as $key => $data)
    <li class="live-users">
      <div>
        <h1>{{$key}}</h1>
      </div>
      <div>
        <h2>{{$data}}</h2>
      </div>
    </li>
    @endforeach
  </ul>
</div>

Integrate Google Analytics Real time Users In Laravel Application, Google Analytics Real time users in Laravel
Conclusion:
In this post, we worked on one of the most controversial aspects of any application- Fetching Real time users from Google Analytics using Laravel. We set up the base in the last post and in this post we dived straight into two use cases. We first worked with Viewing Real-time Users from a single application and discussed viewing real-time users from multiple application.

It may seem complicated in the beginning but hope this tutorial made it easy for you to work step by step without wasting much of your development time.

QUESTIONS & COMMENTS:

Thank you for reading. If you suggestions and questions, share them in the comment section below.

Leave a comment

Your email address will not be published. Required fields are marked *