Stickybeak Docs Dashboard Get early access

Backends

Plain PHP

Plain PHP analytics with Stickybeak starts by adding its script to your rendered pages; this guide shows you where it belongs and how to track page views, goals and purchase revenue. It is a lighter alternative to Google Analytics for a PHP site, with live visitors, goals and revenue attribution in one script.

Add the script

  1. Set the site ID

    Set STICKYBEAK_SITE_ID in your production environment only (in your host's control panel, your Apache or PHP-FPM config, or a .env file your app loads).

  2. Add PHP visitor tracking to your shared header

    Put this before </head>:

    <script>window.stickybeak = window.stickybeak || function () { (window.stickybeak.q = window.stickybeak.q || []).push(arguments); };</script>
    <?php $stickybeakSite = getenv('STICKYBEAK_SITE_ID'); ?>
    <?php if ($stickybeakSite): ?>
      <script
        defer
        data-site="<?= htmlspecialchars($stickybeakSite) ?>"
        data-domain="example.com"
        src="https://app.usestickybeak.com/_cfw/s.js"
      ></script>
    <?php endif; ?>

    No environment variables on your host?

Check it's working

Publish first, then follow Verify your install.

Track goals

Give any button or link a data-goal attribute. See Custom goals.

<button type="submit" data-goal="contact_form">Send</button>

A click goal fires on the click, even if the form then fails validation. To count only forms that went through, use the session below.

Fire a goal after a redirect

When your script handles a form and redirects, put the goal in the session first:

<?php
session_start();

// ...save the signup...

$_SESSION['stickybeak_goal'] = ['name' => 'signup', 'props' => ['plan' => $plan]];
header('Location: /welcome.php', true, 303);
exit;

Then, in the shared header, render it once and remove it.

<?php
$goal = $_SESSION['stickybeak_goal'] ?? null;
unset($_SESSION['stickybeak_goal']);
$js = fn ($v) => json_encode($v, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT);
?>
<?php if ($goal): ?>
  <script>window.stickybeak(<?= $js($goal['name']) ?>, <?= $js((object) ($goal['props'] ?? [])) ?>);</script>
<?php endif; ?>

Track purchases

On your order confirmation, include the amount, currency and order ID. Stickybeak counts each order ID once, so a refreshed thank-you page doesn't double your revenue.

$_SESSION['stickybeak_goal'] = [
    'name'  => 'purchase',
    'props' => ['revenue' => $order['total'], 'currency' => 'USD', 'order_id' => (string) $order['id']],
];

See Revenue.

If it isn't working

  • With PHP-FPM, check clear_env or set the variable in the pool configuration.

See Troubleshooting.