php

how to implement the worlds fastest, easiest and cheapest analytics in your php project

if you get stuck or not feeling confident, i will personally help you integrate it :) book a time on my calendly

composer package (maybe) coming soon

what can i do?

in the meantime you can just call our api (which is exactly what the composer package will be doing). i don't even know if a package is needed since you can just include it in your config.php file.

automatic page view tracking

Create a rustalytics.php file in your includes directory and add the following code. REPLACE <your-api-key> with your api key

<?php
    $api_key = '<your-api-key>';
    $rustalytics_script = "<script>!function(t,e,s,a,i,n,c,p){t[a]=t[a]||function(){(t[a].q=t[a].q||[]).push(arguments)},n=e.createElement(s),c=e.getElementsByTagName(s)[0],n.async=1,n.src=i,c.parentNode.insertBefore(n,c)}(window,document,\"script\",\"rustalytics\",\"https://rustalytics.dev/rustalytics.js\"),rustalytics(\"init\",{apiToken:\"$api_key\"});</script>";

    function track_event($event_name) {
        echo "<script>rustalytics(\"$event_name\");</script>";
    }
    ?>

if you have a config.php file you can require the file in your config file.

<?php
      require_once 'rustalytics.php';
      
      function init_page()
      {
          global $rustalytics_script;
          echo $rustalytics_script;
      }
      
    // ... other config stuff ...
    ?>

include this file in your main template or header file that's used across all pages:

<?php
// In your header.php or main template file
require_once 'path/to/config.php';
?>
<!DOCTYPE html>
<html>
<head>
    <title>My PHP Website</title>
    <?php init_page(); ?>
</head>
<body>
    // ... your page content ...
</body>
</html>

Now, you only need to include your header or main template file in all your pages, and Rustalytics will be automatically included and track page views.

custom events

custom events on ANY html element

After including the rustalytics.php file and calling the function, you can track custom events on ANY html element by adding a data-rustalytics attribute. The value should be the event name you want to track, for example: "clicked-checkout" or if you are a/b testing you can add the variation: "clicked-checkout-variation-a"

<a data-rustalytics="clicked-checkout" href="/checkout.php">checkout</a>

track custom events in PHP

If you want to track a custom event within your PHP code, you can use the following function:

<?php
    function track_event($event_name) {
        echo "<script>rustalytics(\"$event_name\");</script>";
    }
    
    // Usage example:
    track_event("user-registered");
    ?>