Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Analytics Pixel from HTML <head> to Dynamic JavaScript Injection via Backend #3293

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,54 @@

@RestController
@RequestMapping("/js")
public class AdditionalLanguageJsController {
public class AdditionalJsController {

private final LanguageService languageService;

public AdditionalLanguageJsController(LanguageService languageService) {
public AdditionalJsController(LanguageService languageService) {
this.languageService = languageService;
}

@Hidden
@GetMapping(value = "/additionalLanguageCode.js", produces = "application/javascript")
public void generateAdditionalLanguageJs(HttpServletResponse response) throws IOException {
@GetMapping(value = "/additional.js", produces = "application/javascript")
public void generateAdditionalJs(HttpServletResponse response) throws IOException {
Set<String> supportedLanguages = languageService.getSupportedLanguages();
response.setContentType("application/javascript");
PrintWriter writer = response.getWriter();
// Erstelle das JavaScript dynamisch
// Dynamically generate the JavaScript
writer.println(
"const supportedLanguages = "
+ toJsonArray(new ArrayList<>(supportedLanguages))
+ ";");
// Generiere die `getDetailedLanguageCode`-Funktion
// Generate the `getDetailedLanguageCode` function
writer.println(
"""
function getDetailedLanguageCode() {
const userLanguages = navigator.languages ? navigator.languages : [navigator.language];
for (let lang of userLanguages) {
let matchedLang = supportedLanguages.find(supportedLang => supportedLang.startsWith(lang.replace('-', '_')));
if (matchedLang) {
return matchedLang;
}
}
// Fallback
return "en_GB";
function getDetailedLanguageCode() {
const userLanguages = navigator.languages ? navigator.languages : [navigator.language];
for (let lang of userLanguages) {
let matchedLang = supportedLanguages.find(supportedLang => supportedLang.startsWith(lang.replace('-', '_')));
if (matchedLang) {
return matchedLang;
}
""");
}
// Fallback
return "en_GB";
}
""");

writer.println(
"""
// Pixel, doesn't collect any PII
const trackingPixel = document.createElement('img');
trackingPixel.src = 'https://pixel.stirlingpdf.com/a.png?x-pxid=4f5fa02f-a065-4efb-bb2c-24509a4b6b92';
trackingPixel.style.position = 'absolute';
trackingPixel.style.visibility = 'hidden';
document.body.appendChild(trackingPixel);
""");
writer.flush();
}

// Hilfsfunktion zum Konvertieren der Liste in ein JSON-Array
// Helper function to convert list to JSON array
private String toJsonArray(List<String> list) {
StringBuilder jsonArray = new StringBuilder("[");
for (int i = 0; i < list.size(); i++) {
Expand Down
7 changes: 2 additions & 5 deletions src/main/resources/templates/fragments/common.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@
<!-- Bootstrap Icons -->
<link rel="stylesheet" th:href="@{'/css/bootstrap-icons.min.css'}">

<!-- Pixel, doesn't collect any PII-->
<img referrerpolicy="no-referrer-when-downgrade" src="https://pixel.stirlingpdf.com/a.png?x-pxid=4f5fa02f-a065-4efb-bb2c-24509a4b6b92" style="position: absolute; visibility: hidden;"/>

<!-- Custom -->
<link rel="stylesheet" th:href="@{'/css/general.css'}">
<link rel="stylesheet" th:href="@{'/css/theme/theme.css'}">
Expand Down Expand Up @@ -265,7 +262,6 @@
</div>
</div>
<script th:src="@{'/js/fileInput.js'}" type="module"></script>

<div th:if="${@GoogleDriveEnabled == true}" >
<script type="text/javascript" th:src="@{'/js/googleFilePicker.js'}"></script>
<script async defer src="https://apis.google.com/js/api.js" onload="gapiLoaded()"></script>
Expand All @@ -277,4 +273,5 @@
window.stirlingPDF.GoogleDriveAppId = /*[[${@GoogleDriveConfig.getAppId()}]]*/ null;
</script>
</div>
</th:block>
</th:block>

2 changes: 1 addition & 1 deletion src/main/resources/templates/fragments/navbar.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div th:fragment="navbar" class="mx-auto" style="position: sticky; top:0; z-index:10000">
<script th:src="@{'/js/languageSelection.js'}"></script>
<script th:src="@{'/js/navbar.js'}"></script>
<script th:src="@{'/js/additionalLanguageCode.js'}"></script>
<script th:src="@{'/js/additional.js'}"></script>
<script th:inline="javascript">
// Initializing the scripts
initLanguageSettings();
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<th:block th:insert="~{fragments/common :: head(title=#{login.title}, header=#{login.header})}"></th:block>
<link rel="stylesheet" th:href="@{'/css/login.css'}">
<script th:src="@{'/js/languageSelection.js'}"></script>
<script th:src="@{'/js/additionalLanguageCode.js'}"></script>
<script th:src="@{'/js/additional.js'}"></script>
</head>

<body>
Expand Down