Skip to content
Jan edited this page May 10, 2022 · 8 revisions

Getting Started

It is recommended to use SC-Analytics with composer, which is the easiest way to use it. Just add promatur/sc-analytics to your projects requirements.

composer require promatur/sc-analytics

Use some code like this one:

require_once 'vendor/autoload.php';


// - Configure your preferred analytics endpoint
\ScAnalytics\Core\AnalyticsConfig::$matomoID = "1";
\ScAnalytics\Core\AnalyticsConfig::$matomoEndpoint = "https://analytics.example.com/";

// - Initialize the analytics system
\ScAnalytics\Analytics::init();

// - Get your Analytics Handler
$analytics = Analytics::get();

// - Load the Client-Side JavaScript code and initialize the page view
$pageData = new \ScAnalytics\Core\PageData("Help Page", ["Support", "Help Page Overview"])
// Put this anywhere you want to load the JavaScript code
echo $analytics->loadJS($pageData);

Assets

SC-Analytics also provides JavaScript assets. If you want the combination of server-side and client-side analytics, configure the location of the assets in your composer.json:

{
  "extra": {
    "assets-dir": "assets",
    "assets-strategy": "copy"
  }
}

If you want to use another folder for the assets, configure it in the config:

\ScAnalytics\Core\AnalyticsConfig::$assets = "my-directory";

Learn more about the Composer Assets Plugin.

Error handling

This package has an integration of Sentry, which is optional to use. Errors are automatically handled by Sentry and additional information is added.

Requests

There are some requests, which are specific for one type of Analytics Integration. Almost every request is available in each integration. For example an GAExceptionRequest in Google Analytics is implemented using an event in Matomo.

The following requests are automatically handled and the suitable analytics handler is automatically chosen:

$analytics = Analytics::get();

$req = $analytics->event(bool $interactive, string $category, string $action, ?string $label = null, ?int $value = null);
$req = $analytics->exception(?string $description = null, bool $fatal = false);
$req = $analytics->pageView(?PageData $pageData);
$req = $analytics->social(string $network, string $action, string $target);
$req = $analytics->timing(string $group, string $name, int $milliseconds, ?string $label = null);
$req = $analytics->search(?PageData $pageData, string $keyword, int $results, string $category = "all");
$req = $analytics->logout();
$req = $analytics->download(string $fileName, ?int $size = null);

// Don't forget to send the request
$req->send();

Continue

Please look into the PHP documentation, you can find in the code to get more information about every request, parameter and options.

You can also learn more about...

Clone this wiki locally