PHP code example of clamp / analytics

1. Go to this page and download the library: Download clamp/analytics 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/ */

    

clamp / analytics example snippets


use Clamp\Analytics\Analytics;
use Clamp\Analytics\Money;

Analytics::init(
    projectId: 'proj_xxx',
    apiKey: getenv('CLAMP_API_KEY'),
);

// Simple event
Analytics::track('signup', ['plan' => 'pro', 'method' => 'email']);

// Link a server event to a browser visitor
Analytics::track(
    'subscription_started',
    [
        'plan' => 'pro',
        'total' => new Money(29.00, 'USD'),
    ],
    anonymousId: 'aid_xxx',
);

Analytics::track('purchase', [
    'plan' => 'pro',
    'total' => new Money(29.00, 'USD'),
    'tax' => new Money(4.35, 'USD'),
]);

use Clamp\Analytics\Analytics;

try {
    processWebhook($payload);
} catch (\Throwable $e) {
    Analytics::captureError($e, ['webhook' => 'stripe']);
}

try {
    Analytics::track('subscription_started', [...]);
} catch (\Clamp\Analytics\ClampException $e) {
    error_log('failed to send to Clamp: ' . $e->getMessage());
}