From a40fdd5a0be98b62d1268808a59dbd026e7f66df Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com.> Date: Tue, 22 Oct 2024 11:10:09 +0100 Subject: [PATCH] Fixes for analyticsPrompt --- build.gradle | 2 +- .../software/SPDF/config/AppConfig.java | 3 + .../controller/api/SettingsController.java | 1 + src/main/resources/templates/home.html | 68 +++++++++++-------- 4 files changed, 44 insertions(+), 30 deletions(-) diff --git a/build.gradle b/build.gradle index 32e22198..35ca0d6b 100644 --- a/build.gradle +++ b/build.gradle @@ -22,7 +22,7 @@ ext { } group = "stirling.software" -version = "0.30.0" +version = "0.30.1" java { // 17 is lowest but we support and recommend 21 diff --git a/src/main/java/stirling/software/SPDF/config/AppConfig.java b/src/main/java/stirling/software/SPDF/config/AppConfig.java index 3391a50f..75497dba 100644 --- a/src/main/java/stirling/software/SPDF/config/AppConfig.java +++ b/src/main/java/stirling/software/SPDF/config/AppConfig.java @@ -15,6 +15,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Lazy; +import org.springframework.context.annotation.Scope; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; @@ -162,12 +163,14 @@ public class AppConfig { } @Bean(name = "analyticsPrompt") + @Scope("request") public boolean analyticsPrompt() { return applicationProperties.getSystem().getEnableAnalytics() == null || "undefined".equals(applicationProperties.getSystem().getEnableAnalytics()); } @Bean(name = "analyticsEnabled") + @Scope("request") public boolean analyticsEnabled() { if (applicationProperties.getEnterpriseEdition().isEnabled()) return true; return applicationProperties.getSystem().getEnableAnalytics() != null diff --git a/src/main/java/stirling/software/SPDF/controller/api/SettingsController.java b/src/main/java/stirling/software/SPDF/controller/api/SettingsController.java index dd674210..034c6e23 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/SettingsController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/SettingsController.java @@ -32,6 +32,7 @@ public class SettingsController { } GeneralUtils.saveKeyToConfig("system.enableAnalytics", String.valueOf(enabled), false); applicationProperties.getSystem().setEnableAnalytics(String.valueOf(enabled)); + return ResponseEntity.ok("Updated"); } } diff --git a/src/main/resources/templates/home.html b/src/main/resources/templates/home.html index 279b0f6c..ce80a13c 100644 --- a/src/main/resources/templates/home.html +++ b/src/main/resources/templates/home.html @@ -374,10 +374,10 @@

Please consider enabling analytics to help Stirling-PDF grow and to allow us to understand our users better.

You can change the settings for analytics in the config/settings.yml file

- + @@ -426,50 +426,60 @@ - document.addEventListener("DOMContentLoaded", function() { - const surveyVersion = "2.0"; - const modal = new bootstrap.Modal(document.getElementById('surveyModal')); - const dontShowAgain = document.getElementById('dontShowAgain'); - const takeSurveyButton = document.getElementById('takeSurvey'); +document.addEventListener("DOMContentLoaded", function() { + const surveyVersion = "2.0"; + const modal = new bootstrap.Modal(document.getElementById('surveyModal')); + const dontShowAgain = document.getElementById('dontShowAgain'); + const takeSurveyButton = document.getElementById('takeSurvey'); - const viewThresholds = [5, 15, 30, 50, 75, 100, 150, 200]; - let pageViews = parseInt(localStorage.getItem('pageViews') || '0'); + const viewThresholds = [5, 10, 15, 22, 30, 50, 75, 100, 150, 200]; - pageViews++; - localStorage.setItem('pageViews', pageViews.toString()); + // Check if survey version changed and reset page views if it did + const storedVersion = localStorage.getItem('surveyVersion'); + if (storedVersion && storedVersion !== surveyVersion) { + localStorage.setItem('pageViews', '0'); + } - function shouldShowSurvey() { - if (localStorage.getItem('dontShowSurvey') === 'true' || localStorage.getItem('surveyTaken') === 'true') { - return false; + let pageViews = parseInt(localStorage.getItem('pageViews') || '0'); + + pageViews++; + localStorage.setItem('pageViews', pageViews.toString()); + + function shouldShowSurvey() { + if (localStorage.getItem('dontShowSurvey') === 'true' || + localStorage.getItem('surveyTaken') === 'true') { + return false; } - if (localStorage.getItem('surveyVersion') !== surveyVersion) { - return true; + // If survey version changed and we hit a threshold, show the survey + if (localStorage.getItem('surveyVersion') !== surveyVersion && + viewThresholds.includes(pageViews)) { + return true; } return viewThresholds.includes(pageViews); - } + } - if (shouldShowSurvey()) { + if (shouldShowSurvey()) { modal.show(); - } + } - dontShowAgain.addEventListener('change', function() { + dontShowAgain.addEventListener('change', function() { if (this.checked) { - localStorage.setItem('dontShowSurvey', 'true'); - localStorage.setItem('surveyVersion', surveyVersion); + localStorage.setItem('dontShowSurvey', 'true'); + localStorage.setItem('surveyVersion', surveyVersion); } else { - localStorage.removeItem('dontShowSurvey'); - localStorage.removeItem('surveyVersion'); + localStorage.removeItem('dontShowSurvey'); + localStorage.removeItem('surveyVersion'); } - }); + }); - takeSurveyButton.addEventListener('click', function() { + takeSurveyButton.addEventListener('click', function() { localStorage.setItem('surveyTaken', 'true'); localStorage.setItem('surveyVersion', surveyVersion); modal.hide(); - }); }); +});