Skip to content

PHP Example ​

Server-side examples using PHP. These examples show how to send events and query analytics from your backend using OAuth 2.0 authentication.

Authentication ​

Obtain an OAuth token using the client credentials grant:

php
$clientId = 'your-client-id';
$clientSecret = 'your-client-secret';

$ch = curl_init('https://identity.retailsuccessplatform.com/oauth/token');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded'],
    CURLOPT_POSTFIELDS => http_build_query([
        'grant_type' => 'client_credentials',
        'client_id' => $clientId,
        'client_secret' => $clientSecret,
        'scope' => 'insightcore:reports.read insightcore:funnels.read',
    ]),
]);

$response = json_decode(curl_exec($ch), true);
curl_close($ch);

$accessToken = $response['access_token'];

Helper Function ​

All examples below use this helper to make authenticated GET requests:

php
function insightcoreGet(string $path, array $params, string $accessToken): array
{
    $url = 'https://api.insightcore.retailsuccessplatform.com/api/v1' . $path;

    if ($params) {
        $url .= '?' . http_build_query($params);
    }

    $ch = curl_init($url);
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => [
            'Authorization: Bearer ' . $accessToken,
            'Accept: application/json',
        ],
    ]);

    $body = curl_exec($ch);
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($status !== 200) {
        throw new RuntimeException("InsightCore API returned HTTP {$status}: {$body}");
    }

    return json_decode($body, true);
}

Fetch Overview Metrics ​

php
$response = insightcoreGet('/analytics/reports/overview', [
    'from' => '2026-03-01',
    'to' => '2026-03-26',
], $accessToken);

$data = $response['data'];

echo "Visitors: {$data['total_visitors']}\n";
echo "Page Views: {$data['total_page_views']}\n";
echo "Bounce Rate: {$data['bounce_rate']}%\n";
echo "Avg Session: {$data['avg_session_duration_seconds']}s\n";

Fetch Real-Time Visitor Count ​

php
$response = insightcoreGet('/analytics/realtime/active-visitors', [], $accessToken);

echo "Active visitors: {$response['data']['active_visitors']}\n";

Fetch Top Pages ​

php
$response = insightcoreGet('/analytics/reports/pages', [
    'from' => '2026-03-01',
    'to' => '2026-03-26',
], $accessToken);

foreach ($response['data'] as $page) {
    echo "{$page['page_url']}: {$page['views']} views, {$page['unique_visitors']} unique\n";
}

Fetch Traffic Sources ​

php
$response = insightcoreGet('/analytics/reports/traffic-sources', [
    'from' => '2026-03-01',
    'to' => '2026-03-26',
], $accessToken);

foreach ($response['data'] as $source) {
    $campaign = $source['campaign'] ?? '(none)';
    echo "{$source['source']} / {$source['medium']} [{$campaign}]: {$source['visitors']} visitors\n";
}

Fetch Ecommerce Report ​

php
$response = insightcoreGet('/analytics/reports/ecommerce', [
    'from' => '2026-03-01',
    'to' => '2026-03-26',
], $accessToken);

$data = $response['data'];

echo "Revenue: \${$data['total_revenue']}\n";
echo "Orders: {$data['total_orders']}\n";
echo "AOV: \${$data['avg_order_value']}\n";
echo "Conversion Rate: {$data['conversion_rate']}%\n";

Analyze a Funnel ​

php
$funnelId = 'fnl_00000k1L2m3N4o5';

$response = insightcoreGet("/funnels/{$funnelId}/analyze", [
    'from' => '2026-03-01',
    'to' => '2026-03-26',
], $accessToken);

$data = $response['data'];

echo "Funnel: {$data['name']}\n";

foreach ($data['steps'] as $step) {
    $rate = number_format($step['completion_rate'] * 100, 1);
    echo "  Step {$step['step_number']}: {$step['step_name']} — {$step['entries']} entries ({$rate}% completion)\n";
}

Server-Side Event Ingestion ​

Use the authenticated POST /v1/events endpoint to send analytics events from your backend. This is useful for tracking purchases after payment confirmation, recording refunds, importing historical data, or any event that doesn't originate from a browser.

Request a token with the insightcore:events.write scope:

php
$ch = curl_init('https://identity.retailsuccessplatform.com/oauth/token');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded'],
    CURLOPT_POSTFIELDS => http_build_query([
        'grant_type' => 'client_credentials',
        'client_id' => $clientId,
        'client_secret' => $clientSecret,
        'scope' => 'insightcore:events.write',
    ]),
]);

$response = json_decode(curl_exec($ch), true);
curl_close($ch);

$accessToken = $response['access_token'];

Send a Purchase Event ​

php
$payload = json_encode([
    'events' => [
        [
            'type' => 'purchase',
            'url' => 'https://mystore.com/checkout/complete',
            'order_total' => 89.97,
            'items' => [
                ['product_id' => 'prd_00000k1L2m3N4o5', 'quantity' => 3, 'price' => 29.99],
            ],
        ],
    ],
    'ip' => $customerIp,           // optional — end-user IP for GeoIP
    'user_agent' => $customerUa,   // optional — end-user UA string
]);

$ch = curl_init('https://api.insightcore.retailsuccessplatform.com/api/v1/events');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . $accessToken,
        'Content-Type: application/json',
        'Accept: application/json',
    ],
    CURLOPT_POSTFIELDS => $payload,
]);

$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

// $status === 202, $body === {"accepted": 1}

Send a Batch of Events ​

The authenticated endpoint accepts up to 500 events per request:

php
$events = [];

foreach ($orders as $order) {
    $events[] = [
        'type' => 'purchase',
        'url' => "https://mystore.com/orders/{$order->id}",
        'timestamp' => $order->completed_at->getTimestampMs(),
        'order_total' => $order->total,
        'items' => $order->items->map(fn ($item) => [
            'product_id' => $item->product_hashkey,
            'quantity' => $item->quantity,
            'price' => $item->unit_price,
        ])->all(),
    ];
}

$ch = curl_init('https://api.insightcore.retailsuccessplatform.com/api/v1/events');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . $accessToken,
        'Content-Type: application/json',
        'Accept: application/json',
    ],
    CURLOPT_POSTFIELDS => json_encode(['events' => $events]),
]);

$body = json_decode(curl_exec($ch), true);
curl_close($ch);

echo "Accepted: {$body['accepted']} events\n";

Using Guzzle (Laravel / Composer) ​

If your project uses Guzzle, you can simplify the HTTP calls:

php
use GuzzleHttp\Client;

$identity = new Client(['base_uri' => 'https://identity.retailsuccessplatform.com']);

$tokenResponse = $identity->post('/oauth/token', [
    'form_params' => [
        'grant_type' => 'client_credentials',
        'client_id' => $clientId,
        'client_secret' => $clientSecret,
        'scope' => 'insightcore:events.write insightcore:reports.read insightcore:funnels.read',
    ],
]);

$accessToken = json_decode($tokenResponse->getBody(), true)['access_token'];

$insightcore = new Client([
    'base_uri' => 'https://api.insightcore.retailsuccessplatform.com/api/v1/',
    'headers' => [
        'Authorization' => "Bearer {$accessToken}",
        'Accept' => 'application/json',
    ],
]);

// Overview
$overview = json_decode(
    $insightcore->get('analytics/reports/overview', [
        'query' => ['from' => '2026-03-01', 'to' => '2026-03-26'],
    ])->getBody(),
    true
);

echo "Visitors: {$overview['data']['total_visitors']}\n";

// Real-time
$realtime = json_decode(
    $insightcore->get('analytics/realtime/active-visitors')->getBody(),
    true
);

echo "Active now: {$realtime['data']['active_visitors']}\n";

// Top pages
$pages = json_decode(
    $insightcore->get('analytics/reports/pages', [
        'query' => ['from' => '2026-03-01', 'to' => '2026-03-26'],
    ])->getBody(),
    true
);

foreach ($pages['data'] as $page) {
    echo "{$page['page_url']}: {$page['views']} views\n";
}

// Ecommerce
$ecommerce = json_decode(
    $insightcore->get('analytics/reports/ecommerce', [
        'query' => ['from' => '2026-03-01', 'to' => '2026-03-26'],
    ])->getBody(),
    true
);

echo "Revenue: \${$ecommerce['data']['total_revenue']}\n";

// Send events
$insightcore->post('events', [
    'json' => [
        'events' => [
            [
                'type' => 'purchase',
                'url' => 'https://mystore.com/checkout/complete',
                'order_total' => 89.97,
                'items' => [
                    ['product_id' => 'prd_00000k1L2m3N4o5', 'quantity' => 3, 'price' => 29.99],
                ],
            ],
        ],
        'ip' => '203.0.113.42',
    ],
]);

Using Laravel HTTP Client ​

For Laravel applications, you can use the built-in HTTP facade:

php
use Illuminate\Support\Facades\Http;

// Obtain token
$tokenResponse = Http::asForm()->post('https://identity.retailsuccessplatform.com/oauth/token', [
    'grant_type' => 'client_credentials',
    'client_id' => config('services.insightcore.client_id'),
    'client_secret' => config('services.insightcore.client_secret'),
    'scope' => 'insightcore:events.write insightcore:reports.read insightcore:funnels.read',
]);

$accessToken = $tokenResponse->json('access_token');

// Create a reusable client macro or use withToken directly
$api = Http::withToken($accessToken)
    ->baseUrl('https://api.insightcore.retailsuccessplatform.com/api/v1')
    ->acceptJson();

// Overview
$overview = $api->get('/analytics/reports/overview', [
    'from' => '2026-03-01',
    'to' => '2026-03-26',
])->json('data');

// Real-time visitors
$activeVisitors = $api->get('/analytics/realtime/active-visitors')->json('data.active_visitors');

// Top pages
$pages = $api->get('/analytics/reports/pages', [
    'from' => '2026-03-01',
    'to' => '2026-03-26',
])->json('data');

// Ecommerce
$ecommerce = $api->get('/analytics/reports/ecommerce', [
    'from' => '2026-03-01',
    'to' => '2026-03-26',
])->json('data');

// Funnel analysis
$funnel = $api->get('/funnels/fnl_00000k1L2m3N4o5/analyze', [
    'from' => '2026-03-01',
    'to' => '2026-03-26',
])->json('data');

// Send events from backend
$api->post('/events', [
    'events' => [
        [
            'type' => 'purchase',
            'url' => "https://mystore.com/orders/{$order->id}",
            'order_total' => $order->total,
            'items' => $order->items->map(fn ($item) => [
                'product_id' => $item->product_hashkey,
                'quantity' => $item->quantity,
                'price' => $item->unit_price,
            ])->all(),
        ],
    ],
    'ip' => $request->ip(),
]);

Changelog
DateChange
2026-03-28Initial publication with reporting and server-side event ingestion examples.

ShopHero CommerceCore Platform