Dynamic tracking services (#4690)

Added bean for both scarf and posthog
followed documentation at
https://docs.stirlingpdf.com/analytics-telemetry

Co-authored-by: Connor Yoh <connor@stirlingpdf.com>
This commit is contained in:
ConnorYoh 2025-10-16 12:21:52 +01:00 committed by GitHub
parent 06efab5cb2
commit 1a3552d1f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 95 additions and 16 deletions

View File

@ -271,9 +271,14 @@ public class AppConfig {
return "NORMAL";
}
@Bean(name = "disablePixel")
public boolean disablePixel() {
return Boolean.parseBoolean(env.getProperty("DISABLE_PIXEL", "false"));
@Bean(name = "scarfEnabled")
public boolean scarfEnabled() {
return applicationProperties.getSystem().isScarfEnabled();
}
@Bean(name = "posthogEnabled")
public boolean posthogEnabled() {
return applicationProperties.getSystem().isPosthogEnabled();
}
@Bean(name = "machineType")

View File

@ -320,6 +320,8 @@ public class ApplicationProperties {
private String tessdataDir;
private Boolean enableAlphaFunctionality;
private Boolean enableAnalytics;
private Boolean enablePosthog;
private Boolean enableScarf;
private Datasource datasource;
private Boolean disableSanitize;
private int maxDPI;
@ -333,6 +335,18 @@ public class ApplicationProperties {
public boolean isAnalyticsEnabled() {
return this.getEnableAnalytics() != null && this.getEnableAnalytics();
}
public boolean isPosthogEnabled() {
// Treat null as enabled when analytics is enabled
return this.isAnalyticsEnabled()
&& (this.getEnablePosthog() == null || this.getEnablePosthog());
}
public boolean isScarfEnabled() {
// Treat null as enabled when analytics is enabled
return this.isAnalyticsEnabled()
&& (this.getEnableScarf() == null || this.getEnableScarf());
}
}
@Data

View File

@ -56,7 +56,7 @@ public class PostHogService {
}
private void captureSystemInfo() {
if (!applicationProperties.getSystem().isAnalyticsEnabled()) {
if (!applicationProperties.getSystem().isPosthogEnabled()) {
return;
}
try {
@ -67,7 +67,7 @@ public class PostHogService {
}
public void captureEvent(String eventName, Map<String, Object> properties) {
if (!applicationProperties.getSystem().isAnalyticsEnabled()) {
if (!applicationProperties.getSystem().isPosthogEnabled()) {
return;
}

View File

@ -1898,6 +1898,8 @@ cookieBanner.preferencesModal.necessary.title.2=Always Enabled
cookieBanner.preferencesModal.necessary.description=These cookies are essential for the website to function properly. They enable core features like setting your privacy preferences, logging in, and filling out forms—which is why they cant be turned off.
cookieBanner.preferencesModal.analytics.title=Analytics
cookieBanner.preferencesModal.analytics.description=These cookies help us understand how our tools are being used, so we can focus on building the features our community values most. Rest assured—Stirling PDF cannot and will never track the content of the documents you work with.
cookieBanner.preferencesModal.analytics.posthog.label=PostHog Analytics
cookieBanner.preferencesModal.analytics.scarf.label=Scarf Pixel
#scannerEffect
scannerEffect.title=Scanner Effect

View File

@ -1898,6 +1898,8 @@ cookieBanner.preferencesModal.necessary.title.2=Always Enabled
cookieBanner.preferencesModal.necessary.description=These cookies are essential for the website to function properly. They enable core features like setting your privacy preferences, logging in, and filling out forms—which is why they cant be turned off.
cookieBanner.preferencesModal.analytics.title=Analytics
cookieBanner.preferencesModal.analytics.description=These cookies help us understand how our tools are being used, so we can focus on building the features our community values most. Rest assured—Stirling PDF cannot and will never track the content of the documents you work with.
cookieBanner.preferencesModal.analytics.posthog.label=PostHog Analytics
cookieBanner.preferencesModal.analytics.scarf.label=Scarf Pixel
#scannerEffect
scannerEffect.title=Scanner Effect

View File

@ -109,7 +109,9 @@ system:
showUpdateOnlyAdmin: false # only admins can see when a new update is available, depending on showUpdate it must be set to 'true'
customHTMLFiles: false # enable to have files placed in /customFiles/templates override the existing template HTML files
tessdataDir: /usr/share/tessdata # path to the directory containing the Tessdata files. This setting is relevant for Windows systems. For Windows users, this path should be adjusted to point to the appropriate directory where the Tessdata files are stored.
enableAnalytics: null # set to 'true' to enable analytics, set to 'false' to disable analytics; for enterprise users, this is set to true
enableAnalytics: null # Master toggle for analytics: set to 'true' to enable all analytics, 'false' to disable all analytics, or leave as 'null' to prompt admin on first launch
enablePosthog: null # Enable PostHog analytics (open-source product analytics): set to 'true' to enable, 'false' to disable, or 'null' to enable by default when analytics is enabled
enableScarf: null # Enable Scarf pixel: set to 'true' to enable, 'false' to disable, or 'null' to enable by default when analytics is enabled
enableUrlToPDF: false # Set to 'true' to enable URL to PDF, INTERNAL ONLY, known security issues, should not be used externally
disableSanitize: false # set to true to disable Sanitize HTML; (can lead to injections in HTML)
maxDPI: 500 # Maximum allowed DPI for PDF to image conversion

View File

@ -3,6 +3,19 @@ import './cookieconsent.umd.js';
// Enable dark mode
document.documentElement.classList.add('cc--darkmode');
// Build analytics services dynamically based on backend config
const analyticsServices = {};
if (typeof posthogEnabled !== 'undefined' && posthogEnabled) {
analyticsServices.posthog = {
label: cookieBannerPreferencesModalPosthogLabel
};
}
if (typeof scarfEnabled !== 'undefined' && scarfEnabled) {
analyticsServices.scarf = {
label: cookieBannerPreferencesModalScarfLabel
};
}
CookieConsent.run({
guiOptions: {
consentModal: {
@ -22,7 +35,9 @@ CookieConsent.run({
necessary: {
readOnly: true
},
analytics: {}
analytics: {
services: analyticsServices
}
},
language: {
default: "en",

View File

@ -168,14 +168,51 @@
<!-- Bootstrap Icons -->
<link rel="stylesheet" th:href="@{'/css/bootstrap-icons.min.css'}">
<!-- Pixel, doesn't collect any PII-->
<img th:if="${!@disablePixel}" referrerpolicy="no-referrer-when-downgrade"
th:src="'https://pixel.stirlingpdf.com/a.png?x-pxid=4f5fa02f-a065-4efb-bb2c-24509a4b6b92'
+ '&machineType=' + ${@machineType}
+ '&appVersion=' + ${@appVersion}
+ '&licenseType=' + ${@license}
+ '&loginEnabled=' + ${@loginEnabled}"
style="position: absolute; visibility: hidden;" />
<!-- Scarf Pixel - loaded dynamically based on cookie consent -->
<script th:inline="javascript">
const posthogEnabled = /*[[${@posthogEnabled}]]*/ false;
const scarfEnabled = /*[[${@scarfEnabled}]]*/ false;
const scarfPixelUrl = /*[['https://pixel.stirlingpdf.com/a.png?x-pxid=4f5fa02f-a065-4efb-bb2c-24509a4b6b92'
+ '&machineType=' + ${@machineType}
+ '&appVersion=' + ${@appVersion}
+ '&licenseType=' + ${@license}
+ '&loginEnabled=' + ${@loginEnabled}]]*/ '';
function loadScarfPixel() {
if (!scarfEnabled || !window.CookieConsent) return;
// Check if scarf service is accepted
if (window.CookieConsent.acceptedService('scarf', 'analytics')) {
// Remove any existing scarf pixel
const existingPixel = document.getElementById('scarf-pixel');
if (existingPixel) existingPixel.remove();
// Create and inject the pixel
const img = document.createElement('img');
img.id = 'scarf-pixel';
img.src = scarfPixelUrl;
img.referrerPolicy = 'no-referrer-when-downgrade';
img.style.position = 'absolute';
img.style.visibility = 'hidden';
document.body.appendChild(img);
} else {
// Remove pixel if user rejected
const existingPixel = document.getElementById('scarf-pixel');
if (existingPixel) existingPixel.remove();
}
}
// Load pixel on consent events
window.addEventListener('cc:onConsent', loadScarfPixel);
window.addEventListener('cc:onChange', loadScarfPixel);
// Try to load on page load if consent already given
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', loadScarfPixel);
} else {
loadScarfPixel();
}
</script>
<!-- Custom -->
<link rel="stylesheet" th:href="@{'/css/general.css'}">
@ -221,7 +258,7 @@
return;
}
window.CookieConsent.acceptedCategory('analytics')?
window.CookieConsent.acceptedService('posthog', 'analytics')?
posthog.opt_in_capturing() : posthog.opt_out_capturing();
}
const stirlingPDFLabel = /*[[${@StirlingPDFLabel}]]*/ '';
@ -248,6 +285,8 @@
const cookieBannerPreferencesModalNecessaryDescription = /*[[#{cookieBanner.preferencesModal.necessary.description}]]*/ "";
const cookieBannerPreferencesModalAnalyticsTitle = /*[[#{cookieBanner.preferencesModal.analytics.title}]]*/ "";
const cookieBannerPreferencesModalAnalyticsDescription = /*[[#{cookieBanner.preferencesModal.analytics.description}]]*/ "";
const cookieBannerPreferencesModalPosthogLabel = /*[[#{cookieBanner.preferencesModal.analytics.posthog.label}]]*/ "";
const cookieBannerPreferencesModalScarfLabel = /*[[#{cookieBanner.preferencesModal.analytics.scarf.label}]]*/ "";
if (analyticsEnabled) {
!function (t, e) {