# Laravel

## Add Humblytics Analytics to a Laravel Site

### 1 · Sign up (or log in)

Visit humblytics.com → Start Free Trial. Finish signup—or log in to your existing workspace.

### 2 · Add your website in Humblytics

In the sidebar, click **Add Website**.

* **Domain** – enter your site's domain (omit `https://` and `www`)
* **Site Name** – internal label (e.g. `Marketing-Laravel`)

Copy the snippet from **Install Tracking Code**:

html

```html
<!-- Start Humblytics Tracking Code -->
<script async src="https://app.humblytics.com/hmbl.min.js?id=YOUR_ID_HERE"></script>
<!-- End Humblytics Tracking Code -->
```

Keep this tab open—we'll return and click **Verify Website** once the tag is live.

### 3 · Add the script to your Laravel project

#### Option 1: Blade layout (recommended for most apps)

**Best for:** Traditional Blade template apps

1. Open your main layout file, typically:
   * `resources/views/layouts/app.blade.php`
   * Or `resources/views/layouts/master.blade.php`
2. Add your Humblytics script just before the closing `</head>` tag:

blade

```blade
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>{{ config('app.name', 'Laravel') }}</title>
    
    @vite(['resources/css/app.css', 'resources/js/app.js'])
    
    <!-- Humblytics Analytics -->
    @production
    <script async src="https://app.humblytics.com/hmbl.min.js?id=YOUR_ID_HERE"></script>
    @endproduction
</head>
<body>
    @yield('content')
</body>
</html>
```

3. Ensure your pages extend this layout:

blade

```blade
@extends('layouts.app')

@section('content')
    <!-- Your page content -->
@endsection
```

**Note**: The `@production` directive ensures the script only loads in production, not during local development.

#### Option 2: Using environment variables (recommended)

**Best for:** Managing multiple environments (staging, production)

1. Add your Humblytics ID to `.env`:

env

```env
HUMBLYTICS_ID=YOUR_ID_HERE
```

2. In your layout file (`resources/views/layouts/app.blade.php`):

blade

```blade
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>{{ config('app.name', 'Laravel') }}</title>
    
    @vite(['resources/css/app.css', 'resources/js/app.js'])
    
    <!-- Humblytics Analytics -->
    @if(config('app.env') === 'production' && env('HUMBLYTICS_ID'))
    <script async src="https://app.humblytics.com/hmbl.min.js?id={{ env('HUMBLYTICS_ID') }}"></script>
    @endif
</head>
<body>
    @yield('content')
</body>
</html>
```

3. Add to `.env.production` or your production environment

**Note**: Replace `YOUR_ID_HERE` with your actual project ID from Humblytics.

#### Option 3: Blade component (Laravel 8+)

**Best for:** Modern Laravel apps using Blade components

1. Create a new component:

bash

```bash
php artisan make:component Analytics
```

2. Edit `app/View/Components/Analytics.php`:

php

```php
<?php

namespace App\View\Components;

use Illuminate\View\Component;

class Analytics extends Component
{
    public function render()
    {
        return view('components.analytics');
    }
    
    public function shouldRender(): bool
    {
        return app()->environment('production') && config('services.humblytics.id');
    }
}
```

3. Create `resources/views/components/analytics.blade.php`:

blade

```blade
<script async src="https://app.humblytics.com/hmbl.min.js?id={{ config('services.humblytics.id') }}"></script>
```

4. Add to `config/services.php`:

php

```php
'humblytics' => [
    'id' => env('HUMBLYTICS_ID'),
],
```

5. Add to your layout's `<head>`:

blade

```blade
<x-analytics />
```

#### Multiple layouts

If your app uses multiple layout files (e.g., `app.blade.php`, `guest.blade.php`, `admin.blade.php`), add the script to each layout file, or:

1. Create a partial: `resources/views/partials/analytics.blade.php`
2. Add the Humblytics script to this file
3. Include in each layout: `@include('partials.analytics')`

### 4 · Deploy and verify

1. **Clear Laravel caches**:

bash

```bash
   php artisan view:clear
   php artisan config:clear
   php artisan cache:clear
```

2. Deploy your changes to production
3. Return to Humblytics and click **Verify Website**
4. Open your live site in a private/incognito window and refresh once
5. Within \~30 seconds you should see a green **Verified** badge and live visitor count

**If verification fails, check:**

* Script ID matches your project in Humblytics
* You deployed to production (not testing localhost)
* You cleared Laravel's view cache
* The `@production` or environment check allows the script to load
* View page source (right-click → View Page Source) and search for "humblytics"
* No ad-blockers or browser extensions are interfering

**Debug in Laravel:**

bash

```bash
# Check if views are cached
php artisan view:clear

# Check config cache
php artisan config:clear

# Verify environment
php artisan env
```

### 5 · Explore & optimize

* **Dashboard** – see traffic, top pages, and referrers
* **Heatmaps** – auto-generated scroll and click maps
* **Experiments** – run A/B tests directly from Humblytics
