Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: pirsch-analytics/laravel-pirsch
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0.4.5
Choose a base ref
...
head repository: pirsch-analytics/laravel-pirsch
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref

Commits on Mar 31, 2023

  1. Copy the full SHA
    16efb9a View commit details
  2. Add timeout option to Pirsch configurationThis commit adds a timeout …

    …option to the Pirsch configuration, allowing for a customizable timeout for API requests
    xyNNN committed Mar 31, 2023
    Copy the full SHA
    70286c3 View commit details

Commits on Apr 3, 2023

  1. Copy the full SHA
    660b472 View commit details

Commits on Apr 8, 2023

  1. Update config/pirsch.php

    zepfietje authored Apr 8, 2023
    Copy the full SHA
    7db7cea View commit details
  2. Update src/Pirsch.php

    zepfietje authored Apr 8, 2023
    Copy the full SHA
    3d2820a View commit details
  3. Merge pull request #2 from xyNNN/master

    Add configurable timeout to all requests
    zepfietje authored Apr 8, 2023
    Copy the full SHA
    6c1eea1 View commit details
  4. Update changelog

    zepfietje committed Apr 8, 2023
    Copy the full SHA
    4c2433b View commit details

Commits on Aug 2, 2023

  1. Use regular middleware

    zepfietje committed Aug 2, 2023
    Copy the full SHA
    0a0eef5 View commit details

Commits on Sep 22, 2023

  1. Support client hints

    zepfietje committed Sep 22, 2023
    Copy the full SHA
    374532b View commit details

Commits on Feb 21, 2024

  1. Copy the full SHA
    39aa06a View commit details

Commits on Feb 29, 2024

  1. Fixing workflow

    asbiin committed Feb 29, 2024
    Copy the full SHA
    957b4a2 View commit details

Commits on Mar 5, 2024

  1. Merge pull request #7 from asbiin/patch-2

    Fixing workflow
    zepfietje authored Mar 5, 2024
    Copy the full SHA
    f539168 View commit details
  2. Update installation steps

    zepfietje committed Mar 5, 2024
    Copy the full SHA
    5d03511 View commit details
  3. Merge pull request #4 from Autive/master

    Add instruction to generate an access token.
    zepfietje authored Mar 5, 2024
    Copy the full SHA
    e918ffb View commit details
  4. Add Laravel 11 support

    asbiin committed Mar 5, 2024
    Copy the full SHA
    3cb7fb0 View commit details
  5. Merge pull request #5 from asbiin/patch-1

    Add Laravel 11 support
    zepfietje authored Mar 5, 2024
    Copy the full SHA
    4f5cfd9 View commit details
  6. Fix spacing

    zepfietje committed Mar 5, 2024
    Copy the full SHA
    de82f5a View commit details
  7. Update changelog

    zepfietje committed Mar 5, 2024
    Copy the full SHA
    3eb87aa View commit details
  8. Add tests

    asbiin committed Mar 5, 2024
    Copy the full SHA
    d8a68c0 View commit details

Commits on Mar 7, 2024

  1. Copy the full SHA
    3599ff9 View commit details
  2. Copy the full SHA
    f59474e View commit details
  3. Copy the full SHA
    38f6b66 View commit details
  4. Copy the full SHA
    221d918 View commit details

Commits on Mar 8, 2024

  1. fix

    asbiin committed Mar 8, 2024
    Copy the full SHA
    909a415 View commit details
  2. Merge pull request #8 from asbiin/tests01

    Add tests
    zepfietje authored Mar 8, 2024
    Copy the full SHA
    de4d703 View commit details

Commits on Mar 14, 2024

  1. Copy the full SHA
    74659dc View commit details
Showing with 200 additions and 44 deletions.
  1. +3 −1 .github/workflows/phpstan.yml
  2. +14 −11 .github/workflows/run-tests.yml
  3. +28 −0 CHANGELOG.md
  4. +17 −12 README.md
  5. +8 −8 composer.json
  6. +6 −7 src/Http/Middleware/TrackPageview.php
  7. +7 −0 src/Pirsch.php
  8. +0 −5 tests/ExampleTest.php
  9. +69 −0 tests/PirschTest.php
  10. +48 −0 tests/TrackPageviewTest.php
4 changes: 3 additions & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -5,13 +5,15 @@ on:
paths:
- '**.php'
- 'phpstan.neon.dist'
pull_request:
types: [opened, synchronize, reopened]

jobs:
phpstan:
name: phpstan
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
25 changes: 14 additions & 11 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -2,9 +2,9 @@ name: run-tests

on:
push:
branches: [main]
branches: [master]
pull_request:
branches: [main]
types: [opened, synchronize, reopened]

jobs:
test:
@@ -13,25 +13,28 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.1]
laravel: [9.*]
php: ['8.1', '8.2', '8.3']
laravel: ['9.*', '10.*', '11.*']
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 9.*
testbench: 7.*
exclude:
- laravel: '9.*'
stability: prefer-lowest
- laravel: '11.*'
php: '8.1'

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
coverage: none
coverage: pcov
ini-values: pcov.directory=., pcov.exclude="~vendor~"

- name: Setup problem matchers
run: |
@@ -40,8 +43,8 @@ jobs:
- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer require "illuminate/contracts:${{ matrix.laravel }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
- name: Execute tests
run: vendor/bin/pest
run: vendor/bin/pest --coverage
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
# Changelog

## [0.4.9] - 2024-03-05

### Added

- Add Laravel 11 support.

## [0.4.8] - 2023-09-22

### Added

- Support client hints.

## [0.4.7] - 2023-08-02

### Changed

- Use regular middleware.

## [0.4.6] - 2023-04-08

### Changed

- Add timeout of 5 seconds.

## [0.4.5] - 2023-02-14

### Added
@@ -98,6 +122,10 @@

- TrackPageview middleware.

[0.4.9]: https://github.com/pirsch-analytics/laravel-pirsch/releases/tag/0.4.9
[0.4.8]: https://github.com/pirsch-analytics/laravel-pirsch/releases/tag/0.4.8
[0.4.7]: https://github.com/pirsch-analytics/laravel-pirsch/releases/tag/0.4.7
[0.4.6]: https://github.com/pirsch-analytics/laravel-pirsch/releases/tag/0.4.6
[0.4.5]: https://github.com/pirsch-analytics/laravel-pirsch/releases/tag/0.4.5
[0.4.4]: https://github.com/pirsch-analytics/laravel-pirsch/releases/tag/0.4.4
[0.4.3]: https://github.com/pirsch-analytics/laravel-pirsch/releases/tag/0.4.3
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -12,11 +12,19 @@ This package is the official Laravel integration for [Pirsch Analytics](https://
composer require pirsch-analytics/laravel-pirsch
```
2. Add the Pirsch access token to your `.env` file. Leave it empty in non-production environments to disable tracking:
```bash
# ...

PIRSCH_TOKEN=pa_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
```
1. Visit the [Pirsch "Integration" settings page](https://dashboard.pirsch.io/settings/integration).
2. Make sure the correct domain is selected in the top left corner of the page.
3. Scroll down to the "Clients" section and press the "Add Client" button.
4. Select "Access Key (write-only)" as type and enter a description.
5. Press the "Create Client" button and copy the generated "Client secret".
6. Add the copied token to your `.env` file:

```bash
# ...

PIRSCH_TOKEN=pa_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
```

## Usage

@@ -25,17 +33,14 @@ This package is the official Laravel integration for [Pirsch Analytics](https://
#### Automatically

This package comes with a `TrackPageview` middleware that allows you to track pageviews automatically.
Apply the middleware to your web routes by adding it to the `web` key of the `$middlewareGroups` property in your `app/Http/Kernel.php` class:
Apply the middleware to your web routes by appending it in the `withMiddleware` method in your `bootstrap/app.php` file:

```php
protected $middlewareGroups = [
'web' => [
// ...
->withMiddleware(function (Middleware $middleware) {
$middleware->web(append: [
\Pirsch\Http\Middleware\TrackPageview::class,
],

// ...
];
]);
})
```

#### Manually
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -22,21 +22,21 @@
],
"require": {
"php": "^8.1",
"illuminate/contracts": "^9.0|^10.0",
"illuminate/http": "^9.0|^10.0",
"illuminate/contracts": "^9.0|^10.0|^11.0",
"illuminate/http": "^9.0|^10.0|^11.0",
"spatie/laravel-package-tools": "^1.9.2"
},
"require-dev": {
"guzzlehttp/guzzle": "^7.8",
"larastan/larastan": "^2.0.1",
"laravel/pint": "^0.2.2",
"nunomaduro/collision": "^6.0",
"nunomaduro/larastan": "^2.0.1",
"orchestra/testbench": "^7.0",
"pestphp/pest": "^1.21",
"pestphp/pest-plugin-laravel": "^1.1",
"nunomaduro/collision": "^6.0|^7.0|^8.0",
"orchestra/testbench": "^7.0|^8.0|^9.0",
"pestphp/pest-plugin-laravel": "^1.1|^2.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^9.5|^10.0",
"spatie/laravel-ray": "^1.26"
},
"autoload": {
13 changes: 6 additions & 7 deletions src/Http/Middleware/TrackPageview.php
Original file line number Diff line number Diff line change
@@ -11,23 +11,22 @@ class TrackPageview
{
public function handle(Request $request, Closure $next): mixed
{
return $next($request);
}
$response = $next($request);

public function terminate(Request $request, mixed $response): void
{
if ($response instanceof RedirectResponse) {
return;
return $response;
}

if ($request->hasHeader('X-Livewire')) {
return;
return $response;
}

if (str_starts_with($request->route()->uri, 'telescope/')) {
return;
return $response;
}

Pirsch::track();

return $response;
}
}
7 changes: 7 additions & 0 deletions src/Pirsch.php
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ public static function track(

try {
Http::withToken(config('pirsch.token'))
->timeout(5)
->retry(
times: 3,
sleepMilliseconds: 100,
@@ -30,6 +31,12 @@ public static function track(
'ip' => request()->ip(),
'user_agent' => request()->userAgent(),
'accept_language' => request()->header('Accept-Language'),
'sec_ch_ua' => request()->header('Sec-CH-UA'),
'sec_ch_ua_mobile' => request()->header('Sec-CH-UA-Mobile'),
'sec_ch_ua_platform' => request()->header('Sec-CH-UA-Platform'),
'sec_ch_ua_platform_version' => request()->header('Sec-CH-UA-Platform-Version'),
'sec_ch_width' => request()->header('Sec-CH-Width'),
'sec_ch_viewport_width' => request()->header('Sec-CH-Viewport-Width'),
'referrer' => request()->header('Referer'),
...$name === null
? []
5 changes: 0 additions & 5 deletions tests/ExampleTest.php

This file was deleted.

69 changes: 69 additions & 0 deletions tests/PirschTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Pirsch\Facades\Pirsch;

it('can track a request', function () {
config(['pirsch.token' => 'test_token']);
Http::fake([
'https://api.pirsch.io/api/v1/hit' => Http::response(),
]);

Pirsch::track();
$this->get('/');

Http::assertSent(function (Request $request): bool {
expect($request->url())->toBe('https://api.pirsch.io/api/v1/hit');
expect($request->hasHeader('Authorization', 'Bearer test_token'))->toBeTrue();
expect($request->data())->toBe([
'url' => 'http://localhost',
'ip' => '127.0.0.1',
'user_agent' => 'Symfony',
'accept_language' => 'en-us,en;q=0.5',
'sec_ch_ua' => null,
'sec_ch_ua_mobile' => null,
'sec_ch_ua_platform' => null,
'sec_ch_ua_platform_version' => null,
'sec_ch_width' => null,
'sec_ch_viewport_width' => null,
'referrer' => null,
]);

return true;
});
});

it('can send an event', function () {
config(['pirsch.token' => 'test_token']);
Http::fake([
'https://api.pirsch.io/api/v1/event' => Http::response(),
]);

Pirsch::track('name', ['meta' => 'data']);
$this->get('/');

Http::assertSent(function (Request $request) {
expect($request->url())->toBe('https://api.pirsch.io/api/v1/event');
expect($request->hasHeader('Authorization', 'Bearer test_token'))->toBeTrue();
expect($request->data())->toBe([
'url' => 'http://localhost',
'ip' => '127.0.0.1',
'user_agent' => 'Symfony',
'accept_language' => 'en-us,en;q=0.5',
'sec_ch_ua' => null,
'sec_ch_ua_mobile' => null,
'sec_ch_ua_platform' => null,
'sec_ch_ua_platform_version' => null,
'sec_ch_width' => null,
'sec_ch_viewport_width' => null,
'referrer' => null,
'event_name' => 'name',
'event_meta' => [
'meta' => 'data',
],
]);

return true;
});
});
48 changes: 48 additions & 0 deletions tests/TrackPageviewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

use Illuminate\Support\Facades\Route;
use Pirsch\Facades\Pirsch;
use Pirsch\Http\Middleware\TrackPageview;

it('handles request', function () {
Pirsch::shouldReceive('track')
->once();

Route::middleware(TrackPageview::class)
->get('/', fn () => 'Hello World');

$this->get('/');
});

it('skips redirects', function () {
Pirsch::spy();

Route::middleware(TrackPageview::class)
->get('/', fn () => redirect('/home'));

$this->get('/');

Pirsch::shouldNotHaveBeenCalled();
});

it('skips Livewire', function () {
Pirsch::spy();

Route::middleware(TrackPageview::class)
->get('/', fn () => 'Hello World');

$this->get('/', ['X-Livewire' => 'true']);

Pirsch::shouldNotHaveBeenCalled();
});

it('skips Telescope', function () {
Pirsch::spy();

Route::middleware(TrackPageview::class)
->get('telescope/test', fn () => 'Hello World');

$this->get('/telescope/test');

Pirsch::shouldNotHaveBeenCalled();
});