Skip to content

Commit

Permalink
Merge pull request #679 from EPNW/matomo-tag-extensions
Browse files Browse the repository at this point in the history
Several new options for the Matomo Configuration
  • Loading branch information
AltamashShaikh authored Sep 15, 2023
2 parents e3c5a5e + c4e0e5b commit e6b4a13
Show file tree
Hide file tree
Showing 8 changed files with 689 additions and 35 deletions.
46 changes: 40 additions & 6 deletions Template/Tag/MatomoTag.web.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,15 @@
if (!parameters.matomoConfig || !parameters.matomoConfig.name) {
return;
}

// this is the matomoConfig variable name and the only way to differentiate two different tracker
// configurations
var variableName = parameters.matomoConfig.name;

// we need to fetch matomoConfig again in case some parameters changed meanwhile that are variables...
// eg userId might be a variable and it's value might be different now
var matomoConfig = parameters.get('matomoConfig', {});
var trackingEndpoint = matomoConfig.trackingEndpoint == 'custom' ? matomoConfig.trackingEndpointCustom : matomoConfig.trackingEndpoint;
var tracker;
// we make sure to not update jsonConfig even when the configured values change... otherwise we would create
// randomly too many trackers when eg userId changes meanwhile etc
Expand All @@ -109,7 +110,7 @@
lastIdSite = matomoConfig.idSite;
// but even two or more different configs for the same Matomo URL & idSite
lastMatomoUrl = getMatomoUrlFromConfig(matomoConfig);
var trackerUrl = lastMatomoUrl + matomoConfig.trackingEndpoint;
var trackerUrl = lastMatomoUrl + trackingEndpoint;
if (matomoConfig.registerAsDefaultTracker) {
tracker = Piwik.addTracker(trackerUrl, matomoConfig.idSite);
} else {
Expand All @@ -131,6 +132,7 @@

if (matomoConfig.enableCrossDomainLinking) {
tracker.enableCrossDomainLinking();
tracker.setCrossDomainLinkingTimeout(matomoConfig.crossDomainLinkingTimeout);
}

if (matomoConfig.cookieSameSite) {
Expand All @@ -145,6 +147,9 @@
tracker.setCookiePath(matomoConfig.cookiePath);
}

if (matomoConfig.cookieNamePrefix) {
tracker.setCookieNamePrefix(matomoConfig.cookieNamePrefix);
}

if (matomoConfig.cookieDomain) {
tracker.setCookieDomain(matomoConfig.cookieDomain);
Expand Down Expand Up @@ -175,23 +180,48 @@
if (matomoConfig.disableAlwaysUseSendBeacon) {
tracker.disableAlwaysUseSendBeacon();
}

if (matomoConfig.forceRequestMethod) {
tracker.setRequestMethod(matomoConfig.requestMethod);
if(matomoConfig.requestMethod == 'POST'){
tracker.setRequestContentType(matomoConfig.requestContentType);
}
}

if (matomoConfig.enableLinkTracking) {
tracker.enableLinkTracking();
}

if (matomoConfig.enableFileTracking) {
tracker.enableFileTracking();
}

if (matomoConfig.requireConsent) {
tracker.requireConsent();
}

if (matomoConfig.enableDoNotTrack) {
tracker.setDoNotTrack(1);
}

if (matomoConfig.disablePerformanceTracking) {
tracker.disablePerformanceTracking();
}

if (typeof matomoConfig.appendToTrackingUrl === 'string' && matomoConfig.appendToTrackingUrl.length > 0) {
tracker.appendToTrackingUrl(matomoConfig.appendToTrackingUrl);
}

if(typeof matomoConfig.customRequestProcessing === 'function'
&& matomoConfig.customRequestProcessing.length >= 1 ) {
tracker.setCustomRequestProcessing(matomoConfig.customRequestProcessing);
}

if (matomoConfig.enableJSErrorTracking) {
tracker.enableJSErrorTracking();
}
if (matomoConfig.enableHeartBeatTimer) {
tracker.enableHeartBeatTimer();
tracker.enableHeartBeatTimer(matomoConfig.heartBeatTime);
}
if (matomoConfig.trackAllContentImpressions) {
tracker.trackAllContentImpressions();
Expand Down Expand Up @@ -223,7 +253,7 @@
var possiblyUpdatedMatomoUrl = getMatomoUrlFromConfig(matomoConfig);
if (possiblyUpdatedMatomoUrl && lastMatomoUrl !== possiblyUpdatedMatomoUrl) {
// might change each time this method is called
tracker.setTrackerUrl(possiblyUpdatedMatomoUrl + matomoConfig.trackingEndpoint);
tracker.setTrackerUrl(possiblyUpdatedMatomoUrl + trackingEndpoint);
lastIdSite = possiblyUpdatedMatomoUrl;
}

Expand All @@ -250,10 +280,13 @@
var customUrl = parameters.get('customUrl');
if (customUrl) {
tracker.setCustomUrl(customUrl);
}
}
if (matomoConfig.customCookieTimeOutEnable) {
tracker.setVisitorCookieTimeout(matomoConfig.customCookieTimeOut * 86400);
tracker.setReferralCookieTimeout(matomoConfig.referralCookieTimeOut * 86400);
tracker.setSessionCookieTimeout(matomoConfig.sessionCookieTimeOut * 60);
}

tracker.trackPageView();
} else if (trackingType === 'event') {
tracker.trackEvent(parameters.get('eventCategory'), parameters.get('eventAction'), parameters.get('eventName'), parameters.get('eventValue'));
Expand All @@ -278,7 +311,8 @@
}

var matomoUrl = getMatomoUrlFromConfig(matomoConfig);
loadTracker(matomoUrl, matomoConfig.jsEndpoint);
var jsEndpoint = matomoConfig.jsEndpoint == 'custom' ? matomoConfig.jsEndpointCustom : matomoConfig.jsEndpoint;
loadTracker(matomoUrl, jsEndpoint);
};
};
})();
69 changes: 69 additions & 0 deletions Template/Variable/CustomRequestProcessingVariable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/

namespace Piwik\Plugins\TagManager\Template\Variable;

use Piwik\Piwik;
use Piwik\Settings\FieldConfig;
use Piwik\Validators\NotEmpty;

class CustomRequestProcessingVariable extends BaseVariable
{
const ID = 'CustomRequestProcessing';

public function getId()
{
return self::ID;
}

public function getCategory()
{
return self::CATEGORY_OTHERS;
}

public function isCustomTemplate()
{
return true;
}

public function getParameters()
{
return array(
$this->makeSetting('jsFunction', "function(request){ return request; }", FieldConfig::TYPE_STRING, function (FieldConfig $field) {
$field->title = Piwik::translate('TagManager_CustomRequestProcessingVariableJsFunctionTitle');
$field->description = Piwik::translate('TagManager_CustomRequestProcessingVariableJsFunctionDescription');
$field->uiControl = FieldConfig::UI_CONTROL_TEXTAREA;
$field->validators[] = new NotEmpty();
$field->validate = function ($value) {
$value = trim($value);
if (strpos($value, 'function(request){') !== 0) {
throw new \Exception('The value needs to start with "function(request){"');
}
};
$field->transform = function ($value) {
return trim($value);
};
}),
);
}

public function loadTemplate($context, $entity)
{
if (!empty($entity['parameters']['jsFunction'])) {
$function = rtrim(trim($entity['parameters']['jsFunction']), ';');

return '(function () { return function (parameters, TagManager) { this.get = function(){ return ' . $function . '; } ; } })();';
}
}

public function hasAdvancedSettings()
{
return false;
}
}
Loading

0 comments on commit e6b4a13

Please sign in to comment.