Laravel Data-Driven Strategies #4: user navigation tracking and funnel

Kalizi <Andrea>
Dev Genius
Published in
4 min readApr 28, 2021

--

Photo by Shannon Potter on Unsplash

“Unintentional design happens when the organization focuses on the business or the technology, but doesn’t focus on the customer or user experience. The result is an experience that emerges that often isn’t pleasant.” ~ Jared M. Spool

Designing a big system may lead to unexpected behaviours. While feature grows, links between them can become weaker leading to unwanted and inconsistent user experiences damaging your platform usage.

It’s rare to notice unwanted behaviours without knowing how users use your sites, and that’s where analytics join the party.

Lots of people rely on Google Analytics, it gives lots of reports without you having to write a single line of code, others rely on Cloudflare which, like a CDN, records all the traffic, but what if you want to know user behaviours by yourself?

Installing your personal analytics system

There are lots of packages built for Laravel that helps you building your applications faster and easier. The package I choose for here is pragmarx/tracker.

“Storing user tracking information, on indexed and normalized database tables, wastes less disk space and ease the extract of valuable information about your application and business.” ~ Tracker

To start, as with every other package, just:

> composer require pragmarx/tracker

If your Laravel installation is fairly new, you’ll see that at this point, your app already knows about this package:

Discovered Package: pragmarx/tracker

Now publish everything related to this package

> php artisan vendor:publish --provider="PragmaRX\Tracker\Vendor\Laravel\ServiceProvider"Copied File [/vendor/pragmarx/tracker/src/config/config.php] To [/config/tracker.php]
Copied Directory [/vendor/pragmarx/tracker/src/migrations] To [/database/migrations]
Publishing complete.

This will publish 34 migration files (maybe more in the future) and a configuration file.

Now just enable the tracker via its config file ( config/tracker.php ):

'enabled' => true,

The tracker configuration file also has a config file for the database connection to use (from the config/database.php), its default value is tracker so you’ll have to add a connection. I’ll reuse my MySQL connection so it will become

'connection' => 'mysql',

Be sure that MySQL is not in strict mode into the connections key in the database config:

'connections' => [
// ...
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
// ...
'strict' => false,
// ...
],
// ...
],

Finally, migrate everything:

> php artisan migrate
[ ... suppressed output, trust me, it's too long ... ]

Configure tracker

Everything Is Disabled By Default

Tracker is extremely configurable, you can enable/disable everything as you want:

  • cache
  • robots tracking
  • logging for untrackable session
  • HTTP logging
  • console command logging
  • SQL Queries logging
  • SQL Queries binding logging
  • Events
  • GeoIPs
  • User agents
  • Users
  • Devices
  • Languages
  • HTTP Referrers
  • Paths
  • HTTP Queries
  • Routes
  • Exceptions
  • Stats panel

Stats panel

Tracker has a stats panel ready to go with just a few steps:

> git clone https://github.com/BlackrockDigital/startbootstrap-sb-admin-2.git public/templates/sb-admin-2
> cd public/templates/sb-admin-2
> git checkout tags/v3.3.7+1

And obviously… enabling it in config/tracker.php

'stats_panel_enabled' => true,

You can even tweak its base path

'stats_base_uri' => 'stats',

Just authenticated users can see stats, but only if you define authorize them by defining a fake admin attribute via a mutator:

public function getIsAdminAttribute(): bool
{
return true; // your logic here
}

Now open your /stats

Stats page

And after a visit…

Stats page after a visit

Clicking on an ID you can see the full session

User visits

And basically…. that’s all!

GeoIP?

GeoIP refers to the method of locating a computer terminal’s geographic location by identifying that terminal’s IP address.

Using GeoIP can help you locate your visitors by their IP addresses. GeoIP localization is really easy to achieve:

composer require "geoip2/geoip2":"~2.0"

And after the package installation, you can just enable it via config/tracker.php:

'log_geoip' => true,

And now?

Now all your data are there, you just have to analyze, study and combine them, to understand how your users use the site. If you want to know more, you can always query data by yourself, tracker is made of 13 tables you can combine by digging the database structure by yourself or reading the documentation!

Now it’s up to you to build something great! 🥂

Stay tuned for other Data-Driven Strategies and if you want, take a moment ️️to leave a comment about how you take data-driven decisions supported by tools! ☕️

--

--

IT Engineering Bachelor and still student 🎓 Mobile&Backend Dev 💻 Growth hacking Enthusiast ☕ Startupper 🚀 Metalhead 🤘🏻 With love, from Palermo ❤️