-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from companieshouse/feature/IDVA6-1914-Matomo-…
…tracker-client IDVA6-1914 Matomo tracker client
- Loading branch information
Showing
5 changed files
with
261 additions
and
24 deletions.
There are no files selected for viewing
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
80 changes: 80 additions & 0 deletions
80
src/main/java/uk/gov/companieshouse/common/web/service/MatomoClient.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,80 @@ | ||
package uk.gov.companieshouse.common.web.service; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import jakarta.servlet.http.Cookie; | ||
import org.matomo.java.tracking.MatomoRequest; | ||
import org.matomo.java.tracking.MatomoRequests; | ||
import org.matomo.java.tracking.MatomoTracker; | ||
import org.matomo.java.tracking.TrackerConfiguration; | ||
import org.matomo.java.tracking.parameters.VisitorId; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.net.URI; | ||
import java.util.Arrays; | ||
import java.util.Optional; | ||
|
||
@Component | ||
public class MatomoClient { | ||
@Value("${piwik.url}") | ||
private String PIWIK_URL; | ||
@Value("${piwik.siteid}") | ||
private int PIWIK_SITE_ID; | ||
@Value("${service.name}") | ||
private String SERVICE_NAME; | ||
|
||
private MatomoTracker tracker; | ||
|
||
@PostConstruct | ||
public void init() { | ||
// Create the tracker configuration | ||
var configuration = TrackerConfiguration.builder() | ||
.apiEndpoint(URI.create(PIWIK_URL + "/matomo.php")) | ||
.defaultSiteId(PIWIK_SITE_ID) | ||
.build(); | ||
// Prepare the tracker (stateless - can be used for multiple requests) | ||
tracker = new MatomoTracker(configuration); | ||
} | ||
|
||
public void sendEvent(Cookie[] cookies, String page, String eventAction) { | ||
track(cookies, 0, page, eventAction); | ||
} | ||
|
||
public void sendGoal(Cookie[] cookies, int goal) { | ||
track(cookies, goal, null, null); | ||
} | ||
|
||
private void track(Cookie[] cookies, int goal, String page, String eventAction ) { | ||
|
||
// Look for the piwik ID cookie | ||
Optional<Cookie> cookie = Arrays.stream(cookies) | ||
.filter(c ->c.getName().startsWith("_pk_id")) | ||
.findFirst(); | ||
|
||
if (cookie.isPresent()) { | ||
// Only send matomo tracking event if cookie present | ||
|
||
MatomoRequest matomoRequest; | ||
if (goal > 0) { | ||
matomoRequest = MatomoRequests | ||
.goal(goal, null) | ||
.build(); | ||
} else { | ||
// Create the event request - (category, action, name, value) | ||
// Category naming is "service page" | ||
// Action is equivalent of data-event-id in html | ||
matomoRequest = MatomoRequests | ||
.event( SERVICE_NAME + " - " + page, eventAction, null, null) | ||
.build(); | ||
} | ||
|
||
// Get the visitor ID from the cookie | ||
var cookieContents = cookie.get().getValue().split("\\."); | ||
matomoRequest.setVisitorId(VisitorId.fromHex(cookieContents[0])); | ||
|
||
// Send the request asynchronously (non-blocking) as an HTTP POST request (payload is JSON and contains the | ||
// tracking parameters) | ||
tracker.sendRequestAsync(matomoRequest); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
cdn.url=//${CDN_HOST} | ||
piwik.url=${PIWIK_URL} | ||
piwik.siteId=${PIWIK_SITE_ID} | ||
piwik.url=${PIWIK_URL:https://matomo.platform.aws.chdev.org} | ||
piwik.siteId=${PIWIK_SITE_ID:24} | ||
service.name=common-web-java |
94 changes: 94 additions & 0 deletions
94
src/test/java/uk/gov/companieshouse/common/web/unit/service/MatomoClientTest.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,94 @@ | ||
package uk.gov.companieshouse.common.web.unit.service; | ||
|
||
import jakarta.servlet.http.Cookie; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.matomo.java.tracking.MatomoRequest; | ||
import org.matomo.java.tracking.MatomoTracker; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Captor; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
import uk.gov.companieshouse.common.web.service.MatomoClient; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class MatomoClientTest { | ||
|
||
private static final MatomoClient matomoClient=new MatomoClient(); | ||
|
||
private MockHttpServletRequest servletRequest; | ||
|
||
@Mock | ||
MatomoTracker mockTracker; | ||
@Mock | ||
Cookie cookie; | ||
@Captor | ||
private ArgumentCaptor<MatomoRequest> matomoRequestCaptor; | ||
|
||
@BeforeAll | ||
public static void init() { | ||
matomoClient.init(); | ||
} | ||
@BeforeEach | ||
public void setUp() { | ||
servletRequest = new MockHttpServletRequest(); | ||
Cookie[] cookies = new Cookie[]{ cookie }; | ||
servletRequest.setCookies(cookies); | ||
ReflectionTestUtils.setField(matomoClient, "PIWIK_URL", "https://matomo.platform.aws.chdev.org"); | ||
ReflectionTestUtils.setField(matomoClient, "PIWIK_SITE_ID", 24); | ||
ReflectionTestUtils.setField(matomoClient, "SERVICE_NAME", "common-web-java"); | ||
ReflectionTestUtils.setField(matomoClient, "tracker", mockTracker); | ||
} | ||
|
||
@Test | ||
@DisplayName("send goal") | ||
void sendGoalSuccess() { | ||
ReflectionTestUtils.setField(matomoClient, "tracker", mockTracker); | ||
when(cookie.getName()).thenReturn("_pk_id.24.ca6b"); | ||
when(cookie.getValue()).thenReturn("d98f63f0c6f700cd.1732096477."); | ||
|
||
matomoClient.sendGoal(servletRequest.getCookies(), 42); | ||
|
||
verify(mockTracker).sendRequestAsync(matomoRequestCaptor.capture()); | ||
var matomoRequest = matomoRequestCaptor.getValue(); | ||
assertEquals(42, matomoRequest.getGoalId()); | ||
assertEquals("d98f63f0c6f700cd", matomoRequest.getVisitorId().toString()); | ||
} | ||
|
||
@Test | ||
@DisplayName("send event") | ||
void sendEventSuccess() { | ||
when(cookie.getName()).thenReturn("_pk_id.24.ca6b"); | ||
when(cookie.getValue()).thenReturn("d98f63f0c6f700cd.1732096477."); | ||
|
||
matomoClient.sendEvent(servletRequest.getCookies(), "page", "event-action"); | ||
|
||
verify(mockTracker).sendRequestAsync(matomoRequestCaptor.capture()); | ||
var matomoRequest = matomoRequestCaptor.getValue(); | ||
assertEquals("event-action", matomoRequest.getEventAction()); | ||
assertEquals("common-web-java - page", matomoRequest.getEventCategory()); | ||
assertEquals("d98f63f0c6f700cd", matomoRequest.getVisitorId().toString()); | ||
} | ||
|
||
@Test | ||
@DisplayName("send goal") | ||
void noCookie() { | ||
ReflectionTestUtils.setField(matomoClient, "tracker", mockTracker); | ||
when(cookie.getName()).thenReturn("other-cookie"); | ||
|
||
matomoClient.sendGoal(servletRequest.getCookies(), 42); | ||
|
||
verify(mockTracker, times(0)).sendRequestAsync(any()); | ||
} | ||
} |