NAME

Mojolicious::Plugin::Piwik - Use Matomo (Piwik) in Mojolicious

SYNOPSIS

# On startup
plugin 'Piwik' => {
  url => 'piwik.khm.li',
  site_id => 1
};

# In Template
%= piwik_tag

# In controller
my $json = $c->piwik->api('API.getPiwikVersion');

DESCRIPTION

Mojolicious::Plugin::Piwik is a simple plugin for embedding Matomo (Piwik) Analysis in your Mojolicious app. Please respect the privacy of your visitors and do not track more information than necessary!

METHODS

Mojolicious::Plugin::Piwik inherits all methods from Mojolicious::Plugin and implements the following new ones.

register

# Mojolicious
$app->plugin(Piwik => {
  url => 'piwik.khm.li',
  site_id => 1
});

# Mojolicious::Lite
plugin 'Piwik' => {
  url => 'piwik.khm.li',
  site_id => 1
};

# Or in your config file
{
  Piwik => {
    url => 'piwik.khm.li',
    site_id => 1
  }
}

Called when registering the plugin. Accepts the following parameters:

  • url - URL of your Matomo (Piwik) instance.

  • site_id - ID of the site to monitor. Defaults to 1.

  • embed - Activates or deactivates the embedding of Piwik tag. Defaults to a true value if Mojolicious is in production mode, defaults to a false value otherwise. The value is accessible from stash as piwik.embed.

  • token_auth - Token for authentication. Used only for the Piwik API.

  • append - Additional tracking code, that is appended to the code generated by "piwik_tag" and the track_script of the piwik shortcut. Experimental!

All parameters can be set either as part of the configuration file with the key Piwik or on registration (that can be overwritten by configuration).

HELPERS

piwik_tag

%= piwik_tag
%= piwik_tag 1
%= piwik_tag 1, 'piwik.khm.li'
%= piwik_tag 1, 'https://piwik.khm.li'

Renders a script tag that asynchronously loads the Piwik javascript file from your Piwik instance. Accepts optionally a site id and the url of your Piwik instance. Defaults to the site id and the url given when the plugin was registered.

This tag should be included at the bottom of the body tag of each website you want to analyse.

The special as-script tag renders the tracking code for the plugin-wide site_id and url as an external resource. This is required to embed the tracking code in pages with strict Content Security Policies. To make this work, it is necessary to define the track_script shortcut and to add the domain of your Matomo (Piwik) instance to your CSP as a valid script-src and potentially img-src.

# In your application
$app->routes->route('/tracking.js')->piwik('track_script');

# In Template:
%= piwik_tag 'as-script'

The special opt-out tag renders an iframe helping your visitors to disallow tracking via javascript. To make this work with strict Content Security Policies, you need to add the domain of your Matomo (Piwik) instance to your CSP as a valid frame-src.

%= piwik_tag 'opt-out', width => 400

See the default tag helper for explanation of usage.

The special opt-out-link renders an anchor link to the opt-out page to be used if the visitor does not allow third party cookies.

<%= piwik_tag 'opt-out-link', begin %>Opt Out<% end %>
# <a href="..." rel="nofollow">Opt Out</a>

See the default tag helper for explanation of usage.

piwik.api

# In Controller - blocking ...
my $json = $c->piwik->api(
  'Actions.getPageUrl' => {
    token_auth => 'MyToken',
    idSite => [4,7],
    period => 'day',
    date   => 'today'
  }
);

# ... or async
$c->piwik->api(
  'Actions.getPageUrl' => {
    token_auth => 'MyToken',
    idSite => [4,7],
    period => 'day',
    date   => 'today'
  } => sub {
    my $json = shift;
    # ...
  }
);

Sends an API request and returns the response as a hash or array reference (the decoded JSON response). Accepts the API method, a hash reference with request parameters as described in the Piwik API, and optionally a callback, if the request is meant to be non-blocking.

The Tracking API uses the method name Track and will forward user agent and referrer information based on the controller request as well as the url of the requested resource, unless Do-Not-Track is activated. The ip address is not forwarded.

$c->piwik->api(
  Track => {
    idsite => '4',
    res    => [1024, 768],
    action_url  => 'http://khm.li/12',
    action_name => 'Märchen/Rapunzel'
  });

As the url parameter is used to define the Piwik instance, the url of the requested resource has to be named action_url.

Please remember that cookie-based opt-out can't be supported for the non-javascript Tracking API.

In addition to the parameters of the API references, the following parameters are allowed:

  • url - The url of your Piwik instance. Defaults to the url given when the plugin was registered.

  • secure - Boolean value that indicates a request using the https scheme. Defaults to false, in case the url is given without any scheme or with a http scheme.

  • dnt - Override the Do-Not-Track setting, in rare cases, this is required.

idSite is an alias of site_id and idsite and defaults to the id of the plugin registration. Some parameters are allowed to be array references instead of string values, for example idSite (for analysis), date (for ranges) and res (for tracking).

my $json = $c->piwik->api(
  'API.get' => {
    site_id => [4,5],
    period  => 'range',
    date    => ['2012-11-01', '2012-12-01'],
    secure  => 1
  });

In case of an error, piwik.api tries to response with a meaningful description in the hash value of error. If an image is expected instead of a JSON object (as for the Tracking or the ImageGraph API), the image is base64 encoded and mime-type prefixed in the hash value of image, ready to be embedded as the src of an <img /> tag.

piwik.api_p

$c->piwik->api_p(
  'API.get' => {
    site_id => [4,5],
    period  => 'range',
    date    => ['2012-11-01', '2012-12-01'],
    secure  => 1
  }
)->then(
  sub {
    my $res = shift;
    ...
  }
)->wait;

Same as piwik.api, but returns a Mojo::Promise object.

piwik.api_url

my $src_url = $c->piwik->api_url(
  'ImageGraph.get' => {
    apiModule => 'VisitsSummary',
    apiAction => 'get',
    graphType => 'evolution',
    period => 'day',
    date   => 'last30',
    width  => 500,
    height => 250
});

# In template
<img src="<%= $src_url %>" alt="Piwik analysis" />

Creates the URL of an API request and returns the Mojo::URL object. Accepts the same parameters as the piwik.api helper, excluding the callback.

SHORTCUTS

piwik

$app->routes->route('/tracking.js')->piwik('track_script');

Defines a piwik shortcut for routes, accepting the following route names:

  • track_script - Returns a JavaScript file containing the tracking code for site_id and url.

LIMITATIONS

The plugin currently lacks support for eCommerce tracking.

TESTING

To test the plugin against your Piwik instance, create a configuration file with the necessary information as a perl data structure in t/auth.pl and run make test, for example:

{
  token_auth => '123456abcdefghijklmnopqrstuvwxyz',
  url => 'https://piwik.khm.li/',
  site_id => 1,
  action_url => 'http://khm.li/Test',
  action_name => 'Märchen/Test'
};

The user agent to be ignored in your Piwik instance is called Mojo-Test.

DEPENDENCIES

Mojolicious.

AVAILABILITY

https://github.com/Akron/Mojolicious-Plugin-Piwik

PRIVACY NOTE

Please make sure you are using Matomo (Piwik) in compliance to the law. For german users, this information (last accessed on 2021-01-19) may help you to design your service correctly. You may need to inform your users about your usage of Piwik, especially if you are located in the European Union.

COPYRIGHT AND LICENSE

Copyright (C) 2012-2021, Nils Diewald.

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.

This plugin was developed for khm.li - Kinder- und Hausmärchen der Brüder Grimm.