Add custom Matomo analytics integration (#974).

This commit is contained in:
Alex Tselegidis 2022-01-19 10:18:00 +01:00
parent 37163c9bf5
commit 59c44994f7
39 changed files with 154 additions and 68 deletions

View File

@ -71,7 +71,6 @@ $autoload['helper'] = [
'debug',
'env',
'file',
'google_analytics',
'html',
'http',
'installation',

View File

@ -104,6 +104,8 @@ class Booking extends EA_Controller {
$privacy_policy_content = setting('privacy_policy_content');
$display_any_provider = setting('display_any_provider');
$book_advance_timeout = setting('book_advance_timeout');
$google_analytics_code = setting('google_analytics_code');
$matomo_analytics_url = setting('matomo_analytics_url');
$timezones = $this->timezones->to_array();
$grouped_timezones = $this->timezones->to_grouped_array();
@ -123,7 +125,9 @@ class Booking extends EA_Controller {
'page_title' => lang('page_title') . ' ' . $company_name,
'message_title' => lang('appointment_not_found'),
'message_text' => lang('appointment_does_not_exist_in_db'),
'message_icon' => base_url('assets/img/error.png')
'message_icon' => base_url('assets/img/error.png'),
'google_analytics_code' => $google_analytics_code,
'matomo_analytics_url' => $matomo_analytics_url
]);
$this->load->view('pages/booking_message', html_vars());
@ -150,7 +154,9 @@ class Booking extends EA_Controller {
'message_text' => strtr(lang('appointment_locked_message'), [
'{$limit}' => sprintf('%02d:%02d', $hours, $minutes)
]),
'message_icon' => base_url('assets/img/error.png')
'message_icon' => base_url('assets/img/error.png'),
'google_analytics_code' => $google_analytics_code,
'matomo_analytics_url' => $matomo_analytics_url
]);
$this->load->view('pages/booking_message', html_vars());
@ -216,6 +222,8 @@ class Booking extends EA_Controller {
'display_privacy_policy' => $display_privacy_policy,
'privacy_policy_content' => $privacy_policy_content,
'display_any_provider' => $display_any_provider,
'google_analytics_code' => $google_analytics_code,
'matomo_analytics_url' => $matomo_analytics_url,
'timezones' => $timezones,
'grouped_timezones' => $grouped_timezones,
'manage_mode' => $manage_mode,

View File

@ -95,6 +95,8 @@ class Booking_cancellation extends EA_Controller {
'message_title' => lang('appointment_cancelled_title'),
'message_text' => lang('appointment_cancelled'),
'message_icon' => base_url('assets/img/success.png'),
'google_analytics_code' => setting('google_analytics_code'),
'matomo_analytics_url' => setting('matomo_analytics_url'),
'exceptions' => $exceptions
]);

View File

@ -86,6 +86,8 @@ class Booking_confirmation extends EA_Controller {
html_vars([
'page_title' => lang('success'),
'google_analytics_code' => setting('google_analytics_code'),
'matomo_analytics_url' => setting('matomo_analytics_url'),
]);
$this->load->view('pages/booking_confirmation', html_vars());

View File

@ -1,59 +0,0 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Online Appointment Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.1.0
* ---------------------------------------------------------------------------- */
/**
* Print Google Analytics script.
*
* This helper function should be used in view files in order to output the Google Analytics script. It will check
* whether the code is set in the database and print it, otherwise nothing will be outputted. This eliminates the need
* for extra checking before outputting.
*/
function google_analytics_script()
{
$google_analytics_code = setting('google_analytics_code');
if ($google_analytics_code !== '')
{
// If the Google Analytics code starts with UA then it is a Universal Analytics Property and the script stays
// the legacy one.
if (substr($google_analytics_code, 0, 2) === 'UA')
{
echo '
<script>
(function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,"script","//www.google-analytics.com/analytics.js","ga");
ga("create", "' . $google_analytics_code . '", "auto");
ga("send", "pageview");
</script>
';
}
// If the Google Analytics code starts with a G then it is a Google Analytics 4-Property and the script
// to inject it looks different.
if (substr($google_analytics_code, 0, 2) === 'G-')
{
echo '
<script async src="https://www.googletagmanager.com/gtag/js?id=' . $google_analytics_code . '"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag("js", new Date());
gtag("config", "' . $google_analytics_code . '");
</script>
';
}
}
}

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -286,7 +286,7 @@ $lang['time_format'] = 'Time Format';
$lang['time_format_hint'] = 'Change the time display format (H - Hours, M - Minutes).';
$lang['first_weekday'] = 'First day of week';
$lang['first_weekday_hint'] = 'Set the first day of the calendar week.';
$lang['google_analytics_code_hint'] = 'Add your Google Analytics ID to be included in the booking page. You can use the tracking id or measurement id.';
$lang['google_analytics_code_hint'] = 'Add your Google Analytics Code to enable Google Analytics tracking on the booking pages.';
$lang['availabilities_type'] = 'Availabilities Type';
$lang['flexible'] = 'Flexible';
$lang['fixed'] = 'Fixed';
@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -282,7 +282,7 @@ $lang['time_format'] = 'Zeitformat';
$lang['time_format_hint'] = 'Ändern Sie das Zeitanzeigeformat (H - Stunden, M - Minuten).';
$lang['first_weekday'] = 'Erster Tag der Woche';
$lang['first_weekday_hint'] = 'Wählen Sie den ersten Tag der Kalenderwoche.';
$lang['google_analytics_code_hint'] = 'Fügen Sie Ihre Google Analytics-ID hinzu, das auf der Buchungsseite enthalten wird.';
$lang['google_analytics_code_hint'] = 'Fügen Sie Ihre Google Analytics-Code hinzu, um das Google Analytics tracking auf den Buchungsseiten zu aktivieren.';
$lang['availabilities_type'] = 'Verfügungstyp';
$lang['flexible'] = 'Flexibel';
$lang['fixed'] = 'Fest';

View File

@ -286,7 +286,7 @@ $lang['time_format'] = 'Μορφή Ώρας';
$lang['time_format_hint'] = 'Αλλάξτε την μορφή ώρας (H - Ώρα, M - Λεπτά).';
$lang['first_weekday'] = 'Πρώτη ημέρα της εβδομάδας';
$lang['first_weekday_hint'] = 'Καθορίστε την πρώτη ημέρα της εβδομάδας στο ημερολόγιο.';
$lang['google_analytics_code_hint'] = 'Προσθέστε τον Google Analytics ID σας το οποίο θα συμπεριληφθεί στην σελίδα κράτησης.';
$lang['google_analytics_code_hint'] = 'Προσθέστε τον Google Analytics Code σας για να ενεργοποιήσετε το Google Analytics tracking στις σελίδες κράτησης.';
$lang['availabilities_type'] = 'Τύπος Διαθεσιμοτήτων';
$lang['flexible'] = 'Ευέλικτος';
$lang['fixed'] = 'Σταθερός';
@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -336,4 +336,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -337,4 +337,5 @@ $lang['booking_settings'] = 'Booking Settings';
$lang['display'] = 'Display';
$lang['require'] = 'Require';
$lang['color'] = 'Color';
$lang['matomo_analytics_url_hint'] = 'Add the URL to your own Matomo installation to enable Matomo tracking on the booking pages.';
// End

View File

@ -0,0 +1,41 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Online Appointment Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.4.0
* ---------------------------------------------------------------------------- */
class Migration_Add_matomo_analytics_url_setting extends EA_Migration {
/**
* Upgrade method.
*/
public function up()
{
if ( ! $this->db->get_where('settings', ['name' => 'matomo_analytics_url'])->num_rows())
{
$this->db->insert('settings', [
'name' => 'matomo_analytics_url',
'value' => ''
]);
}
}
/**
* Downgrade method.
*/
public function down()
{
if ( ! $this->db->get_where('settings', ['name' => 'matomo_analytics_url'])->num_rows())
{
$this->db->delete('settings', [
'name' => 'matomo_analytics_url',
]);
}
}
}

View File

@ -0,0 +1,27 @@
<?php
/**
* @var string $google_analytics_code
*/
?>
<?php if (substr($google_analytics_code, 0, 2) === 'UA'): ?>
<script>
(function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,"script","//www.google-analytics.com/analytics.js","ga");
ga("create", "<?= $google_analytics_code ?>", "auto");
ga("send", "pageview");
</script>
<?php endif ?>
<?php if (substr($google_analytics_code, 0, 2) === 'G-'): ?>
<script async src="https://www.googletagmanager.com/gtag/js?id=' . $google_analytics_code . '"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag("js", new Date());
gtag("config", "<?= $google_analytics_code ?>");
</script>
<?php endif ?>

View File

@ -0,0 +1,24 @@
<?php
/**
* @var string $matomo_analytics_url
*/
?>
<script>
var _paq = window._paq = window._paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
_paq.push(["setDocumentTitle", document.domain + "/" + document.title]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function () {
var u = "<?= $matomo_analytics_url ?>";
_paq.push(['setTrackerUrl', u + 'matomo.php']);
_paq.push(['setSiteId', '1']);
var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0];
g.async = true;
g.src = u + 'matomo.js';
s.parentNode.insertBefore(g, s);
})();
</script>
<noscript><p><img src="<?= $matomo_analytics_url ?>matomo.php?idsite=1&amp;rec=1" style="border:0;" alt=""/></p>
</noscript>

View File

@ -71,7 +71,8 @@
<?php component('js_vars_script') ?>
<?php component('js_lang_script') ?>
<?php google_analytics_script() ?>
<?php component('google_analytics_script', ['google_analytics_code' => vars('google_analytics_code')]) ?>
<?php component('matomo_analytics_script', ['matomo_analytics_url' => vars('matomo_analytics_url')]) ?>
<?php slot('scripts') ?>

View File

@ -62,7 +62,8 @@
<?php component('js_vars_script') ?>
<?php component('js_lang_script') ?>
<?php google_analytics_script() ?>
<?php component('google_analytics_script', ['google_analytics_code' => vars('google_analytics_code')]) ?>
<?php component('matomo_analytics_script', ['matomo_analytics_url' => vars('matomo_analytics_url')]) ?>
<?php slot('scripts') ?>

View File

@ -111,7 +111,8 @@
<div class="col-lg-6">
<div class="mb-3">
<label class="form-label" for="google-analytics-code">
Google Analytics ID</label>
Google Analytics Code
</label>
<input id="google-analytics-code" placeholder="UA-XXXXXXXX-XX or G-XXXXXXXXXX"
data-field="google_analytics_code" class="form-control">
<div class="form-text text-muted">
@ -120,6 +121,18 @@
</small>
</div>
</div>
<div class="mb-3">
<label class="form-label" for="google-analytics-code">
Matomo Analytics URL
</label>
<input id="matomo-analytics-url" placeholder="//example.org/url/to/matomo/"
data-field="matomo_analytics_url" class="form-control">
<div class="form-text text-muted">
<small>
<?= lang('matomo_analytics_url_hint') ?>
</small>
</div>
</div>
<div class="mb-3">
<label class="form-label" for="api-token">API Token</label>
<input id="api-token" data-field="api_token" class="form-control">