forked from DSpace/DSpace
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CST-18963] Adds matomo enricher related to the cookie that identifie…
…s a visitor
- Loading branch information
1 parent
5497013
commit 611f353
Showing
6 changed files
with
132 additions
and
16 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
...ce-api/src/main/java/org/dspace/matomo/factory/MatomoRequestCookieIdentifierEnricher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* The contents of this file are subject to the license and copyright | ||
* detailed in the LICENSE and NOTICE files at the root of the source | ||
* tree and available online at | ||
* | ||
* http://www.dspace.org/license/ | ||
*/ | ||
package org.dspace.matomo.factory; | ||
|
||
import java.util.Optional; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Stream; | ||
|
||
import jakarta.servlet.http.Cookie; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.dspace.matomo.model.MatomoRequestDetails; | ||
import org.dspace.usage.UsageEvent; | ||
|
||
/** | ||
* Enricher that extracts any {@code _pk_id} cookie sent inside the request | ||
* to track the same {@code id} used to track interaction on the angular side. <br> | ||
* The cookie will have a similar format: {@code _pk_id.1.1fff=3225aebdb98b13f9.1740076196.} <br> | ||
* Where only the first 16 hexadecimal characters represents the id for Matomo. | ||
* | ||
* @author Vincenzo Mecca (vins01-4science - vincenzo.mecca at 4science.com) | ||
**/ | ||
public class MatomoRequestCookieIdentifierEnricher implements MatomoRequestDetailsEnricher { | ||
|
||
static final String _PK_ID_NAME = "_pk_id"; | ||
static final Pattern _pk_id = Pattern.compile("(^([a-f]|[0-9]){16})"); | ||
|
||
@Override | ||
public MatomoRequestDetails enrich(UsageEvent usageEvent, MatomoRequestDetails matomoRequestDetails) { | ||
Cookie[] cookies = usageEvent.getRequest().getCookies(); | ||
if (cookies == null || cookies.length == 0) { | ||
return matomoRequestDetails; | ||
} | ||
Optional<String> pk_id = | ||
Stream.of(cookies) | ||
.filter(cookie -> cookie.getName().startsWith(_PK_ID_NAME)) | ||
.map(cookie -> _pk_id.matcher(cookie.getValue())) | ||
.filter(Matcher::find) | ||
.findFirst() | ||
.map(Matcher::group); | ||
return pk_id.filter(StringUtils::isNotEmpty) | ||
.map(id -> matomoRequestDetails.addParameter("_id", id)) | ||
.orElse(matomoRequestDetails); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters