Skip to content

Commit

Permalink
New: Make tracking from domains with subdomains possible
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnitto committed Feb 18, 2021
1 parent 70d5ac3 commit 91b1c7b
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 42 deletions.
46 changes: 31 additions & 15 deletions Classes/EelHelper/PlausibleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

namespace Carbon\Plausible\EelHelper;

use Neos\Flow\Annotations as Flow;
use Neos\Eel\ProtectedContextAwareInterface;
use JShrink\Minifier;
use Neos\Eel\ProtectedContextAwareInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Http\Helper\RequestInformationHelper;
use Psr\Http\Message\ServerRequestInterface;


/**
* @Flow\Proxy(false)
*/
class PlausibleHelper implements ProtectedContextAwareInterface
{

/**
* Minimze JavaScript
*
Expand All @@ -23,28 +27,40 @@ public function minifyJS(string $javascript): string
}

/**
* Get main domain from domain string
* Remove protocol and trailing slash, and return the domain without subdomain
* Return domain without protocol and trailing slash
*
* @param string $domain
* @param ServerRequestInterface $request
* @return string
*/
public function mainDomain(string $domain): string
public function getDomain(ServerRequestInterface $request): string
{
$domain = (string)RequestInformationHelper::generateBaseUri($request);
// Remove protocol and trailing slash
$number = preg_match('/\/\/([^\/]*)/', (string)$domain, $matches);
$number = preg_match('/\/\/([^\/]*)/', $domain, $matches);
if ($number) {
$domain = $matches[1];
} else {
// Remove trailing slash
$number = preg_match('/([^\/]*)/', (string)$domain, $matches);
$domain = $matches[1];
return $matches[1];
}
// Remove trailing slash
$number = preg_match('/([^\/]*)/', $domain, $matches);
return $matches[1];
}

/**
* Checks if the requested domain fits into the configured domain
*
* @param ServerRequestInterface $request
* @param string $configuredDomain
* @return bool
*/
public function checkDomain(ServerRequestInterface $request, string $configuredDomain): bool
{
$domain = $this->getDomain($request);
$slice = count(explode('.', $configuredDomain)) * -1;

// Get domain without subdomain
// Get domain in the same format as configured ($configuredDomain)
$array = explode('.', $domain);
$array = array_slice($array, -2);
return implode('.', $array);
$array = array_slice($array, $slice);
return implode('.', $array) === $configuredDomain;
}

/**
Expand Down
4 changes: 0 additions & 4 deletions Resources/Private/Fusion/Component/RequestDomain.fusion

This file was deleted.

6 changes: 2 additions & 4 deletions Resources/Private/Fusion/Component/TrackingCode.fusion
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ prototype(Carbon.Plausible:Component.TrackingCode) < prototype(Neos.Fusion:Compo

renderer = Neos.Fusion:Component {
@apply.props = ${props}
requestMainDomain = Carbon.Plausible:Component.RequestMainDomain {
@if.shouldCheck = ${props.checkIfDomainIsTheSameAsRequest}
}


dataExclude = ${Type.isString(props.exclusions) ? props.exclusions : Array.join(props.exclusions, ',')}
dataExclude.@if {
Expand All @@ -52,7 +50,7 @@ prototype(Carbon.Plausible:Component.TrackingCode) < prototype(Neos.Fusion:Compo

@if {
hasDomain = ${this.domain}
domainIsTheSameAsRequestDomain = ${props.checkIfDomainIsTheSameAsRequest ? props.domain == this.requestMainDomain : true}
domainCheck = ${props.checkIfDomainIsTheSameAsRequest ? Carbon.Plausible.checkDomain(request.httpRequest, props.domain) : true}
}

renderer = afx`
Expand Down
2 changes: 1 addition & 1 deletion Resources/Private/Fusion/Document/SetCookie.fusion
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ prototype(Carbon.Plausible:Document.SetCookie) < prototype(Neos.Fusion:Case) {
setCookie {
condition = true
renderer = Neos.Fusion:Component {
domain = Carbon.Plausible:Component.RequestMainDomain
domain = ${Carbon.Plausible.getDomain(request.httpRequest)}
title = ${Translation.translate('disableCookie', 'Tracking for {0} from this browser is now disabled', [this.domain], null, 'Carbon.Plausible')}
cookieJS = Neos.Fusion:ResourceUri {
path = 'resource://Carbon.Plausible/Public/Cookie.js'
Expand Down
6 changes: 3 additions & 3 deletions Resources/Private/Modules/Views/Index.fusion
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Carbon.Plausible.PlausibleController {
settings = ${Configuration.setting('Carbon.Plausible')}
default = ${this.settings.default}

requestMainDomain = Carbon.Plausible:Component.RequestMainDomain
requestDomain = ${Carbon.Plausible.getDomain(request.httpRequest)}
disableBecauseOfCookie = ${!!request.httpRequest.cookieParams.disabledPlausible}

isAdministrator = ${Security.hasRole('Neos.Neos:Administrator')}
Expand Down Expand Up @@ -36,10 +36,10 @@ Carbon.Plausible.PlausibleController {
</h2>

<p @if.set={props.disableBecauseOfCookie}>
{Translation.translate('cookieStatus.disabled', 'Tracking from {0} is <strong>disabled</strong> in this browser.', [props.requestMainDomain], 'Module', 'Carbon.Plausible')}
{Translation.translate('cookieStatus.disabled', 'Tracking from {0} is <strong>disabled</strong> in this browser.', [props.requestDomain], 'Module', 'Carbon.Plausible')}
</p>
<p @if.set={!props.disableBecauseOfCookie}>
{Translation.translate('cookieStatus.enabled', 'Tracking from {0} is <strong>enabled</strong> in this browser.', [props.requestMainDomain], 'Module', 'Carbon.Plausible')}
{Translation.translate('cookieStatus.enabled', 'Tracking from {0} is <strong>enabled</strong> in this browser.', [props.requestDomain], 'Module', 'Carbon.Plausible')}
</p>

<section @if.set={!props.isAdministrator} class='plausible-domainlist'>
Expand Down
48 changes: 34 additions & 14 deletions Resources/Public/Cookie.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
function setCookie() {
// cookie will die in +100 years from now
// 1 * 60 * 60 * 24 * 365 * 100 = 31536000000
cookie(31536000000);
}

function deleteCookie() {
cookie(-1);
}

function cookie(maxAge) {
var domain = document.location.host.split(".").slice(-2).join(".");
document.cookie = "disabledPlausible=true; SameSite=Lax; path=/; max-age=" + maxAge + "; domain=" + domain;
}
(function () {
window.setCookie = function () {
// cookie will die in +100 years from now
// 1 * 60 * 60 * 24 * 365 * 100 = 31536000000
cookie(31536000000);
};

window.deleteCookie = function () {
cookie(-1);
};

var host = document.location.host;
var mainDomain = host.split(".").slice(-2).join(".");
var value = "disabledPlausible=true";

function set(maxAge, domain) {
document.cookie = value + "; SameSite=Lax; path=/; max-age=" + maxAge + "; domain=" + domain;
}

function check(maxAge) {
var isSet = document.cookie.indexOf(value) !== -1;
var shouldBeSet = maxAge !== -1;
if ((shouldBeSet && !isSet) || (!shouldBeSet && isSet)) {
set(maxAge, host);
}
}

function cookie(maxAge) {
set(maxAge, mainDomain);
if (host != mainDomain) {
check(maxAge);
}
}
})();
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "GPL-3.0",
"keywords": ["flow", "neos", "analytics", "plausible", "carbon"],
"require": {
"neos/neos": "^5.0 || ^7.0",
"neos/neos": "^5.3 || ^7.0",
"neos/fusion-afx": "*",
"tedivm/jshrink": "^1.4"
},
Expand Down

0 comments on commit 91b1c7b

Please sign in to comment.