Developer corner

Target group: Developers

Objects

A data object is available for use in the PSR-14 events:

JavaScriptCode

The Brotkrueml\MatomoIntegration\Code\JavaScriptCode object holds a piece of arbitrary JavaScript code used in the BeforeTrackPageViewEvent, AfterTrackPageViewEvent and AddToDataLayerEvent events. This object is necessary to distinguish between a “normal” string and JavaScript code for later embedding.

Example:

$javaScriptCode = new Brotkrueml\MatomoIntegration\Code\JavaScriptCode(
   '/* some JavaScript code */'
);

The object provides the following method:

__toString(): string

Returns the JavaScript code.

PSR-14 events

To enrich Matomo’s JavaScript tracking code with additional calls, PSR-14 events are available. You can draw inspiration from the Use cases chapter.

See also

You can find more information about PSR-14 events in the blog article PSR-14 Events in TYPO3 and the official TYPO3 documentation.

EnrichScriptTagEvent

New in version 1.3.0.

With this event you can add attributes to the surrounding <script> tag. For a concrete usage have a look into the use cases.

The event provides the following methods:

setId(string $id): void

Set the id.

setType(string $type): void

Set the type.

addDataAttribute(string $name, string $value = ''): void

Add a data attribute with the $name without the data- prefix. The value is optional, if it is not given or an empty string only the name is rendered.

Example

The example below results in the following script snippet:

<script id="some-id" data-foo="bar" data-qux>/* the tracking code */</script>
  1. Create the event listener

    <?php
    declare(strict_types=1);
    
    namespace YourVender\YourExtension\EventListener;
    
    use Brotkrueml\MatomoIntegration\Event\EnrichScriptTagEvent;
    
    final class AddAttributesToMatomoScriptTag
    {
       public function __invoke(EnrichScriptTagEvent $event): void
       {
          // Set the id
          $event->setId('some-id');
    
          // Add data attributes
          $event->addDataAttribute('foo', 'bar');
          $event->addDataAttribute('qux');
       }
    }
    
  2. Register your event listener in Configuration/Services.yaml

    services:
       YourVendor\YourExtension\EventListener\AddAttributesToMatomoScriptTag:
          tags:
             - name: event.listener
               identifier: 'addAttributesToMatomoScriptTag'
               # The event tag can be omitted for TYPO3 v11+
               event: Brotkrueml\MatomoIntegration\Event\EnrichScriptTagEvent
    

BeforeTrackPageViewEvent

This event can be used to add calls before the embedding of the trackPageView code.

This can be helpful when you want to adjust the document title or to add custom dimensions.

The event provides the following methods:

addJavaScriptCode(string $code): void

Adds a JavaScript code snippet.

addMatomoMethodCall(string $method, ...$parameters): void

Adds a Matomo method call for the given method and optional parameters. The value can be of type: array, bool, int, float, string or Brotkrueml\MatomoIntegration\Code\JavaScriptCode.

Example

The example below results in the following code:

// ...
_paq.push(["setDocumentTitle", "Some Document Title"]);
_paq.push(["trackPageView"]);
// ...

or (for illustration of the usage of the Brotkrueml\MatomoIntegration\Code\JavaScriptCode object):

// ...
function getDocumentTitle { return "Some Document Title"; }
_paq.push(["setDocumentTitle", getDocumentTitle()]);
_paq.push(["trackPageView"]);
// ...
  1. Create the event listener

    <?php
    declare(strict_types=1);
    
    namespace YourVender\YourExtension\EventListener;
    
    use Brotkrueml\MatomoIntegration\Event\BeforeTrackPageViewEvent;
    
    final class SetDocumentTitleExample
    {
       public function __invoke(BeforeTrackPageViewEvent $event): void
       {
          // Set the document title
          $event->addMatomoMethodCall('setDocumentTitle', 'Some Document Title');
    
          // OR:
          // Add some JavaScript code
          $event->addJavaScriptCode('function getDocumentTitle { return "Some Document Title"; }');
          // Set the document title
          $event->addMatomoMethodCall('setDocumentTitle', new JavaScriptCode('getDocumentTitle()');]);
       }
    }
    
  2. Register your event listener in Configuration/Services.yaml

    services:
       YourVendor\YourExtension\EventListener\SetDocumentTitleExample:
          tags:
             - name: event.listener
               identifier: 'setDocumentTitleExample'
               # The event tag can be omitted for TYPO3 v11+
               event: Brotkrueml\MatomoIntegration\Event\BeforeTrackPageViewEvent
    

TrackSiteSearchEvent

New in version 1.3.0.

The event is useful for tracking site search metrics, such as the keyword or the number of results. Especially the number of results can be interesting, since Matomo displays a list of keywords without results.

Further information can be found on the Matomo website:

Important

If this event is used and the keyword is not empty, the default trackPageView call is replaced by a trackSiteSearch call, as recommended by Matomo.

The event provides the following methods:

setKeyword(string $keyword): void

Sets the keyword.

setCategory(string|false $category): void

Sets an optional category.

setSearchCount(int|false $searchCount): void

Sets an optional search count.

addCustomDimension(int $id, string $value): void

Adds a custom dimension with the given ID and value.

Example

The example below results in the following code:

// ...
_paq.push(["trackSiteSearch", "some search keyword", false, 42, {"dimension3": "Some custom dimension value"}]);
// ...
  1. Create the event listener

    <?php
    declare(strict_types=1);
    
    namespace YourVender\YourExtension\EventListener;
    
    use Brotkrueml\MatomoIntegration\Event\TrackSiteSearchEvent;
    
    final class SomeTrackSiteSearchExample
    {
       public function __invoke(TrackSiteSearchEvent $event): void
       {
          $event->setKeyword('some search keyword');
          $event->setSearchCount(42);
          $event->addCustomDimension(3, 'some custom dimension value');
       }
    }
    
  2. Register your event listener in Configuration/Services.yaml

    services:
       YourVendor\YourExtension\EventListener\SomeTrackSiteSearchExample:
          tags:
             - name: event.listener
               identifier: 'someTrackSiteSearchExample'
               # The event tag can be omitted for TYPO3 v11+
               event: Brotkrueml\MatomoIntegration\Event\TrackSiteSearchEvent
    

EnrichTrackPageViewEvent

This event can be used to enrich the trackPageView call with a page title and/or a custom dimension only for the page view.

The event provides the following methods:

setPageTitle(string $pageTitle): void

Sets the page title.

addCustomDimension(int $id, string $value): void

Adds a custom dimension with the given ID and value.

Example

The example below results in the following code:

// ...
_paq.push(["trackPageView", "Some Page Title", {"dimension3": "Some Custom Dimension Value"}]);
// ...
  1. Create the event listener

    <?php
    declare(strict_types=1);
    
    namespace YourVender\YourExtension\EventListener;
    
    use Brotkrueml\MatomoIntegration\Event\EnrichTrackPageViewEvent;
    
    final class SomeEnrichTrackPageViewExample
    {
       public function __invoke(EnrichTrackPageViewEvent $event): void
       {
          // You can set another page title
          $event->setPageTitle('Some Page Title');
          // And/or you can set a custom dimension only for the track page view call
          $event->addCustomDimension(3, 'Some Custom Dimension Value');
       }
    }
    
  2. Register your event listener in Configuration/Services.yaml

    services:
       YourVendor\YourExtension\EventListener\SomeEnrichTrackPageViewExample:
          tags:
             - name: event.listener
               identifier: 'someEnrichTrackPageViewExample'
               # The event tag can be omitted for TYPO3 v11+
               event: Brotkrueml\MatomoIntegration\Event\EnrichTrackPageViewEvent
    

AfterTrackPageViewEvent

This event can be used to add calls after the embedding of the trackPageView code.

The event provides the following methods:

addJavaScriptCode(string $code): void

Adds a JavaScript code snippet.

addMatomoMethodCall(string $method, ...$parameters): void

Adds a Matomo method call for the given method and optional parameters. The value can be of type: array, bool, int, float, string or Brotkrueml\MatomoIntegration\Code\JavaScriptCode.

Example

The example below results in the following code:

// ...
_paq.push(["trackPageView"]);
_paq.push(["enableHeartBeatTimer", 30]);
// ...
  1. Create the event listener

    <?php
    declare(strict_types=1);
    
    namespace YourVender\YourExtension\EventListener;
    
    use Brotkrueml\MatomoIntegration\Event\AfterTrackPageViewEvent;
    
    final class EnableHeartBeatTimerWithActiveSecondsExample
    {
       public function __invoke(AfterTrackPageViewEvent $event): void
       {
          $event->addMatomoMethodCall('enableHeartBeatTimer', 30);
       }
    }
    
  2. Register your event listener in Configuration/Services.yaml

    services:
       YourVendor\YourExtension\EventListener\EnableHeartBeatTimerWithActiveSecondsExample:
          tags:
             - name: event.listener
               identifier: 'enableHeartBeatTimerWithActiveSecondsExample'
               # The event tag can be omitted for TYPO3 v11+
               event: Brotkrueml\MatomoIntegration\Event\BeforeTrackPageViewEvent
    

AddToDataLayerEvent

With this event you can add variables to the Matomo tag manager data layer.

The event provides the following method:

addVariable(string $name, $value): void

Adds a variable with a name and value. The value can be of type: string, int, float or Brotkrueml\MatomoIntegration\Code\JavaScriptCode.

Example

The example below results in the following code:

var _mtm=window._mtm||[];
_mtm.push({"mtm.startTime": (new Date().getTime()), "event": "mtm.Start", "orderTotal": 4545.45, "orderCurrency": "EUR"});
// ...

The mtm.startTime and event variables are added always by default.

  1. Create the event listener

    <?php
    declare(strict_types=1);
    
    namespace YourVender\YourExtension\EventListener;
    
    use Brotkrueml\MatomoIntegration\Event\AddToDataLayerEvent;
    
    final class AddOrderDetailsToDataLayerExample
    {
        public function __invoke(AddToDataLayerEvent $event): void
        {
            $event->addVariable('orderTotal', 4545.45);
            $event->addVariable('orderCurrency', 'EUR');
        }
    }
    
  2. Register your event listener in Configuration/Services.yaml

    services:
       YourVendor\YourExtension\EventListener\AddOrderDetailsToDataLayerExample:
          tags:
             - name: event.listener
               identifier: 'addOrderDetailsToDataLayerExample'
               # The event tag can be omitted for TYPO3 v11+
               event: Brotkrueml\MatomoIntegration\Event\AddToDataLayerEvent