PHP code example of runomine / runomine-php

1. Go to this page and download the library: Download runomine/runomine-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

runomine / runomine-php example snippets


use Rom; // or Runomine\Laravel\Facades\Rom

Rom::track('signup');

Rom::track('purchase', ['plan' => 'rent', 'film' => 'the-third-man'], ['amount' => 4.99, 'currency' => 'EUR']);

// Your own payment webhook, for payments no connected provider sees (see "Revenue from your payment provider"):
// no browser behind it, but you know who paid.
Rom::track('purchase', ['plan' => 'yearly'], ['amount' => 49, 'currency' => 'EUR'], user: $order->user_id);

// Hand the visitor to a queued job when it should count as the same visit.
ProcessCheckout::dispatch($order, Rom::visitor());
// ...and in the job:
Rom::track('checkout_completed', context: ['visitor' => $this->visitor]);

$request->attributes->set('runomine.props', ['section' => 'news']);              // the middleware sends them

Rom::pageview($request, ['props' => ['section' => 'news', 'area' => 'public']]);  // or by hand



$rom = new Runomine\Client(); // reads RUNOMINE_TOKEN, RUNOMINE_URL, RUNOMINE_ENABLED
// or: new Runomine\Client(token: 'rm_live_...');

// In your front controller, for HTML pages:
$rom->pageview([
    'url' => (($_SERVER['HTTPS'] ?? '') === 'on' ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],
    'referrer' => $_SERVER['HTTP_REFERER'] ?? null,
    'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? null,
    'ip' => $_SERVER['REMOTE_ADDR'],            // hashed here, never sent
    'country' => $_SERVER['HTTP_CF_IPCOUNTRY'] ?? null,
    'user' => $_SESSION['user_id'] ?? null,
    'props' => ['section' => 'news'],         // optional, stored as the page view's dims
]);

// Anywhere:
$rom->track('purchase', ['plan' => 'rent'], ['amount' => 4.99, 'currency' => 'EUR'], user: '42', context: [
    'url' => 'https://example.com/checkout/success',
    'ip' => $_SERVER['REMOTE_ADDR'],
    'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? '',
]);

// Optional: send now. Otherwise it flushes at 500 events, after 5 seconds, on destruct and at shutdown.
if (function_exists('fastcgi_finish_request')) {
    fastcgi_finish_request(); // let the visitor go first
}
$rom->flush();

$ok = Rom::sample(['queue' => ['pending' => 3, 'failed' => 0], 'healthy' => true]);       // or $client->sample(...)
$ok = Rom::sample(['cpu' => 0.12], new DateTimeImmutable('2026-10-14 10:00:00Z'));        // measured earlier

// A catch-up, in calls of at most 500:
$ok = $client->samples([['ts' => '2026-10-14T10:00:00Z', 'data' => ['cpu' => 0.1]], ['ts' => '2026-10-14T10:01:00Z', 'data' => ['cpu' => 0.2]]]);

Rom::checkoutMetadata();          // ['runomine_visitor' => '5f9e1bebe31e8bf5', 'runomine_user' => '42']
Rom::checkoutMetadata($team->id); // your own id instead of the logged-in user's

// Plain PHP: ip, user agent, host and user
$rom->checkoutMetadata($_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT'] ?? '', $_SERVER['HTTP_HOST'], $userId);

$metadata = Rom::checkoutMetadata();

$session = $stripe->checkout->sessions->create([
    'mode' => 'subscription',
    'line_items' => [['price' => 'price_123', 'quantity' => 1]],
    'success_url' => route('billing.done'),
    'client_reference_id' => (string) $user->id,
    'metadata' => $metadata,
    'subscription_data' => ['metadata' => $metadata], // renewals
]);

// Without Checkout, on the PaymentIntent:
$stripe->paymentIntents->create(['amount' => 499, 'currency' => 'eur', 'metadata' => $metadata]);

'attributes' => ['checkout_data' => ['custom' => Rom::checkoutMetadata()]],

// or a link:
$url = 'https://store.lemonsqueezy.com/checkout/buy/VARIANT?'.http_build_query(['checkout' => ['custom' => Rom::checkoutMetadata()]]);

$attributes = collect(Rom::checkoutMetadata())->map(fn ($value, $key) => ['key' => $key, 'value' => $value])->values();
// cartCreate(input: { lines: [...], attributes: $attributes })

'purchase_units' => [[
    'amount' => ['currency_code' => 'EUR', 'value' => '4.99'],
    'custom_id' => http_build_query(Rom::checkoutMetadata()),
]],

use Runomine\Laravel\Facades\Rom;

public function test_buying_tracks_a_purchase(): void
{
    $rom = Rom::fake();

    $this->actingAs($user)->post('/checkout', ['plan' => 'rent']);

    $rom->assertTracked('purchase', fn (array $event) => $event['revenue'] === ['amount' => 4.99, 'currency' => 'EUR']
        && $event['props']['plan'] === 'rent'
        && $event['user'] === (string) $user->id);
    $rom->assertTrackedTimes('purchase', 1);
    $rom->assertNotTracked('refund');
}

public function test_the_home_page_counts(): void
{
    $rom = Rom::fake();

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

    $rom->assertPageviewRecorded(fn (array $event) => str_ends_with($event['url'], '/'));
}

$sender = new Runomine\Testing\FakeSender();          // or new FakeSender([503, 202]) to script answers
$client = new Runomine\Client(token: 'rm_live_test', sender: $sender);

$client->track('signup');
$client->flush();

$sender->events();   // the events, as sent
$sender->samples();  // the samples, as sent
$sender->requests(); // url, headers, body of every call
sh
php artisan vendor:publish --tag=runomine-config