Skip to content

Example Implementation

Jan edited this page May 10, 2022 · 4 revisions

On this page you can find a complete example integration for tracking page views on a Hello World application including content impressions using Matomo.

<?php

use ScAnalytics\Analytics;
use ScAnalytics\Core\AnalyticsConfig;
use ScAnalytics\Core\PageData;
use ScAnalytics\Core\Scope;

// Load composer packages
require_once 'vendor/autoload.php';

// Configure Matomo endpoint
AnalyticsConfig::$matomoEndpoint = "https://analytics.website.com";
AnalyticsConfig::$matomoID = "1";
AnalyticsConfig::$matomoToken = "31d817d2415ae07cb606472bece75662";

// Configure and initialize the analytics system
AnalyticsConfig::$version = "1.0.0";
// Important: Implement proper consent management system
if (($_GET['consent'] ?? '0') === '1') {
    $_SESSION['consent'] = true;
}
Analytics::init(new Scope(explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE'])[0], [], isset($_SESSION['consent'])));
?>
<html lang="en">
<head>
    <title>Matomo Test Page</title>
    <?php
    // Important: Load JS and send page view data
    echo Analytics::get()->loadJS(new PageData("Matomo Test Page", ["SC-Analytics Demo"]));
    ?>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<div class="container mx-auto h-full">
    <h1>Matomo Test Page</h1>
    <h2>Tracking content impressions</h2>
    <div class="grid grid-cols-2 gap-2 h-full content-center text-center">
        <div data-track-content data-content-ignoreinteraction data-content-name="Test"
             data-content-piece="piece1">Tracked content 1
        </div>
        <div data-track-content data-content-ignoreinteraction data-content-name="Test"
             data-content-piece="piece2">Tracked content 2
        </div>
    </div>
    <h2>Send custom events</h2>
    <div class="grid grid-cols-2 gap-2 text-center">
        <a href="#" onclick="sendAnalyticsEvent('click', 'Test', 'a')">Send Event a</a>
        <a href="#" onclick="sendAnalyticsEvent('click', 'Test', 'b')">Send Event b</a>
    </div>
</div>
</body>
</html>
Clone this wiki locally