Skip to content

Commit 5f27ab2

Browse files
authored
Merge pull request #63 from dheid/master
Add request builder
2 parents 19adc0c + 8b456c9 commit 5f27ab2

39 files changed

+4239
-3508
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

README.md

Lines changed: 267 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,336 @@
1-
Matomo Java Tracker
2-
================
1+
# Matomo Java Tracker
32

43
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.piwik.java.tracking/matomo-java-tracker/badge.svg?style=flat)](https://maven-badges.herokuapp.com/maven-central/org.piwik.java.tracking/matomo-java-tracker)
5-
## Code Status
64
[![Build Status](https://travis-ci.org/matomo-org/matomo-java-tracker.svg?branch=master)](https://travis-ci.org/matomo-org/matomo-java-tracker)
75
[![Average time to resolve an issue](https://isitmaintained.com/badge/resolution/matomo-org/matomo-java-tracker.svg)](https://isitmaintained.com/project/matomo-org/matomo-java-tracker "Average time to resolve an issue")
86
[![Percentage of issues still open](https://isitmaintained.com/badge/open/matomo-org/matomo-java-tracker.svg)](https://isitmaintained.com/project/matomo-org/matomo-java-tracker "Percentage of issues still open")
97

108
Official Java implementation of the [Matomo Tracking HTTP API](https://developer.matomo.org/api-reference/tracking-api).
119

1210
## Javadoc
13-
The Javadoc for this project is hosted as a Github page for this repo. The latest Javadoc can be found [here](https://matomo-org.github.io/matomo-java-tracker/javadoc/HEAD/index.html). Javadoc for the latest and all releases can be found [here](https://matomo-org.github.io/matomo-java-tracker/javadoc/index.html).
11+
12+
The Javadoc for this project is hosted as a Github page for this repo. The latest Javadoc can be
13+
found [here](https://matomo-org.github.io/matomo-java-tracker/javadoc/HEAD/index.html). Javadoc for the latest and all
14+
releases can be found [here](https://matomo-org.github.io/matomo-java-tracker/javadoc/index.html).
1415

1516
## Using this API
16-
### Create a Request
17-
Each PiwikRequest represents an action the user has taken that you want tracked by your Piwik server. Create a PiwikRequest through
18-
```java
19-
PiwikRequest request = new PiwikRequest(siteId, actionUrl);
17+
18+
### Add library to your build
19+
20+
Add a dependency on Matomo Java Tracker using Maven:
21+
22+
```xml
23+
24+
<dependency>
25+
<groupId>org.piwik.java.tracking</groupId>
26+
<artifactId>matomo-java-tracker</artifactId>
27+
<version>2.0</version>
28+
</dependency>
29+
```
30+
31+
or Gradle:
32+
33+
```groovy
34+
dependencies {
35+
implementation("org.piwik.java.tracking:matomo-java-tracker:2.0")
36+
}
2037
```
2138

22-
The following parameters are also enabled by default:
39+
### Create a Request
40+
41+
Each MatomoRequest represents an action the user has taken that you want tracked by your Matomo server. Create a
42+
MatomoRequest through
2343

2444
```java
25-
required = true;
26-
visitorId = random 16 character hex string;
27-
randomValue = random 20 character hex string;
28-
apiVersion = 1;
29-
responseAsImage = false;
45+
46+
import org.matomo.java.tracking.MatomoRequest;
47+
48+
public class YourImplementation {
49+
50+
public void yourMethod() {
51+
MatomoRequest request = MatomoRequest.builder()
52+
.siteId(42)
53+
.actionUrl("https://www.mydomain.com/signup")
54+
.actionName("Signup")
55+
.build();
56+
}
57+
58+
}
59+
3060
```
3161

62+
Per default every request has the following default parameters:
63+
64+
| Parameter Name | Default Value |
65+
|------------------|--------------------------------|
66+
| required | true |
67+
| visitorId | random 16 character hex string |
68+
| randomValue | random 20 character hex string |
69+
| apiVersion | 1 |
70+
| responseAsImage | false |
71+
3272
Overwrite these properties as desired.
3373

34-
Note that if you want to be able to track campaigns using <em>Referrers &gt; Campaigns</em>, you must add the correct URL parameters to your actionUrl. For example,
74+
Note that if you want to be able to track campaigns using *Referrers &gt; Campaigns*, you must add the correct
75+
URL parameters to your actionUrl. For example,
76+
3577
```java
36-
URL actionUrl = new URL("http://example.org/landing.html?pk_campaign=Email-Nov2011&pk_kwd=LearnMore");
78+
79+
package example;
80+
81+
import org.matomo.java.tracking.MatomoRequest;
82+
83+
public class YourImplementation {
84+
85+
public void yourMethod() {
86+
87+
MatomoRequest request = MatomoRequest.builder()
88+
.siteId(42)
89+
.actionUrl("http://example.org/landing.html?pk_campaign=Email-Nov2011&pk_kwd=LearnMore") // include the query parameters to the url
90+
.actionName("LearnMore")
91+
.build();
92+
}
93+
94+
}
3795
```
96+
3897
See [Tracking Campaigns](https://matomo.org/docs/tracking-campaigns/) for more information.
3998

40-
All HTTP query parameters denoted on the [Matomo Tracking HTTP API](https://developer.matomo.org/api-reference/tracking-api) can be set using the appropriate getters and setters. See <strong>PiwikRequest.java</strong> for the mappings of the parameters to their corresponding Java getters/setters.
99+
All HTTP query parameters denoted on
100+
the [Matomo Tracking HTTP API](https://developer.matomo.org/api-reference/tracking-api) can be set using the appropriate
101+
getters and setters. See _MatomoRequest.java_ for the mappings of the parameters to their corresponding
102+
Java getters/setters.
41103

42104
Some parameters are dependent on the state of other parameters:
43-
<strong>EcommerceEnabled</strong> must be called before the following parameters are set: <em>EcommerceId</em> and <em>EcommerceRevenue</em>.
105+
_EcommerceEnabled_ must be called before the following parameters are set: *EcommerceId* and *
106+
EcommerceRevenue*.
44107

45-
<strong>EcommerceId</strong> and <strong>EcommerceRevenue</strong> must be set before the following parameters are set: <em>EcommerceDiscount</em>, <em>EcommerceItem</em>, <em>EcommerceLastOrderTimestamp</em>, <em>EcommerceShippingCost</em>, <em>EcommerceSubtotal</em>, and <em>EcommerceTax</em>.
108+
_EcommerceId_ and _EcommerceRevenue_ must be set before the following parameters are
109+
set: *EcommerceDiscount*, *EcommerceItem*, *EcommerceLastOrderTimestamp*, *
110+
EcommerceShippingCost*, *EcommerceSubtotal*, and *EcommerceTax*.
46111

47-
<strong>AuthToken</strong> must be set before the following parameters are set: <em>VisitorCity</em>, <em>VisitorCountry</em>, <em>VisitorIp</em>, <em>VisitorLatitude</em>, <em>VisitorLongitude</em>, and <em>VisitorRegion</em>.
112+
_AuthToken_ must be set before the following parameters are set: *VisitorCity*, *
113+
VisitorCountry*, *VisitorIp*, *VisitorLatitude*, *VisitorLongitude*, and *VisitorRegion*
114+
.
48115

49116
### Sending Requests
50-
Create a PiwikTracker through
117+
118+
Create a MatomoTracker using the constructor
119+
51120
```java
52-
PiwikTracker tracker = new PiwikTracker(hostUrl);
121+
package example;
122+
123+
import org.matomo.java.tracking.MatomoTracker;
124+
125+
public class YourImplementation {
126+
127+
public void yourMethod() {
128+
129+
MatomoTracker tracker = new MatomoTracker("https://your-matomo-domain.tld/matomo.php");
130+
131+
}
132+
133+
}
53134
```
54-
where hostUrl is the url endpoint of the Piwik server. Usually in the format http://your-piwik-domain.tld/piwik.php.
135+
136+
using the Matomo Endpoint URL as the first parameter.
55137

56138
To send a single request, call
139+
57140
```java
58-
Future<HttpResponse> response = tracker.sendRequestAsync(request);
141+
package example;
142+
143+
import org.apache.http.HttpResponse;
144+
import org.matomo.java.tracking.MatomoRequest;
145+
import org.matomo.java.tracking.MatomoTracker;
146+
147+
import java.io.IOException;
148+
import java.io.UncheckedIOException;
149+
import java.util.concurrent.ExecutionException;
150+
import java.util.concurrent.Future;
151+
152+
public class YourImplementation {
153+
154+
public void yourMethod() {
155+
156+
MatomoRequest request = MatomoRequest.builder().siteId(42).actionUrl("https://www.mydomain.com/some/page").actionName("Signup").build();
157+
158+
MatomoTracker tracker = new MatomoTracker("https://your-matomo-domain.tld/matomo.php");
159+
try {
160+
Future<HttpResponse> response = tracker.sendRequestAsync(request);
161+
// usually not needed:
162+
HttpResponse httpResponse = response.get();
163+
int statusCode = httpResponse.getStatusLine().getStatusCode();
164+
if (statusCode > 399) {
165+
// problem
166+
}
167+
} catch (IOException e) {
168+
throw new UncheckedIOException("Could not send request to Matomo", e);
169+
} catch (ExecutionException | InterruptedException e) {
170+
throw new RuntimeException("Error while getting response", e);
171+
}
172+
173+
}
174+
175+
}
59176
```
60177

61-
If you have multiple requests to wish to track, it may be more efficient to send them in a single HTTP call. To do this, send a bulk request. Place your requests in an <strong>Iterable</strong> data structure and call
178+
If you have multiple requests to wish to track, it may be more efficient to send them in a single HTTP call. To do this,
179+
send a bulk request. Place your requests in an _Iterable_ data structure and call
180+
62181
```java
63-
Future<HttpResponse> response = tracker.sendBulkRequestAsync(requests);
182+
package example;
183+
184+
import org.apache.http.HttpResponse;
185+
import org.matomo.java.tracking.MatomoRequest;
186+
import org.matomo.java.tracking.MatomoRequestBuilder;
187+
import org.matomo.java.tracking.MatomoTracker;
188+
189+
import java.io.IOException;
190+
import java.io.UncheckedIOException;
191+
import java.util.ArrayList;
192+
import java.util.Collection;
193+
import java.util.concurrent.ExecutionException;
194+
import java.util.concurrent.Future;
195+
196+
public class YourImplementation {
197+
198+
public void yourMethod() {
199+
200+
Collection<MatomoRequest> requests = new ArrayList<>();
201+
MatomoRequestBuilder builder = MatomoRequest.builder().siteId(42);
202+
requests.add(builder.actionUrl("https://www.mydomain.com/some/page").actionName("Some Page").build());
203+
requests.add(builder.actionUrl("https://www.mydomain.com/another/page").actionName("Another Page").build());
204+
205+
MatomoTracker tracker = new MatomoTracker("https://your-matomo-domain.tld/matomo.php");
206+
try {
207+
Future<HttpResponse> response = tracker.sendBulkRequestAsync(requests);
208+
// usually not needed:
209+
HttpResponse httpResponse = response.get();
210+
int statusCode = httpResponse.getStatusLine().getStatusCode();
211+
if (statusCode > 399) {
212+
// problem
213+
}
214+
} catch (IOException e) {
215+
throw new UncheckedIOException("Could not send request to Matomo", e);
216+
} catch (ExecutionException | InterruptedException e) {
217+
throw new RuntimeException("Error while getting response", e);
218+
}
219+
220+
}
221+
222+
}
223+
64224
```
65-
If some of the parameters that you've specified in the bulk request require AuthToken to be set, this can also be set in the bulk request through
225+
226+
If some of the parameters that you've specified in the bulk request require AuthToken to be set, this can also be set in
227+
the bulk request through
228+
66229
```java
67-
Future<HttpResponse> response = tracker.sendBulkRequestAsync(requests, authToken);
230+
package example;
231+
232+
import org.apache.http.HttpResponse;
233+
import org.matomo.java.tracking.MatomoLocale;
234+
import org.matomo.java.tracking.MatomoRequest;
235+
import org.matomo.java.tracking.MatomoRequestBuilder;
236+
import org.matomo.java.tracking.MatomoTracker;
237+
238+
import java.io.IOException;
239+
import java.io.UncheckedIOException;
240+
import java.util.ArrayList;
241+
import java.util.Collection;
242+
import java.util.Locale;
243+
import java.util.concurrent.ExecutionException;
244+
import java.util.concurrent.Future;
245+
246+
public class YourImplementation {
247+
248+
public void yourMethod() {
249+
250+
Collection<MatomoRequest> requests = new ArrayList<>();
251+
MatomoRequestBuilder builder = MatomoRequest.builder().siteId(42);
252+
requests.add(builder.actionUrl("https://www.mydomain.com/some/page").actionName("Some Page").build());
253+
requests.add(builder.actionUrl("https://www.mydomain.com/another/page").actionName("Another Page").visitorCountry(new MatomoLocale(Locale.GERMANY)).build());
254+
255+
MatomoTracker tracker = new MatomoTracker("https://your-matomo-domain.tld/matomo.php");
256+
try {
257+
Future<HttpResponse> response = tracker.sendBulkRequestAsync(requests, "33dc3f2536d3025974cccb4b4d2d98f4"); // second parameter is authentication token need for country override
258+
// usually not needed:
259+
HttpResponse httpResponse = response.get();
260+
int statusCode = httpResponse.getStatusLine().getStatusCode();
261+
if (statusCode > 399) {
262+
// problem
263+
}
264+
} catch (IOException e) {
265+
throw new UncheckedIOException("Could not send request to Matomo", e);
266+
} catch (ExecutionException | InterruptedException e) {
267+
throw new RuntimeException("Error while getting response", e);
268+
}
269+
270+
}
271+
272+
}
273+
274+
68275
```
69-
## Install
276+
277+
## Building
278+
279+
You need a GPG signing key on your machine. Please follow these
280+
instructions: https://docs.github.com/en/authentication/managing-commit-signature-verification/generating-a-new-gpg-key
281+
70282
This project can be tested and built by calling
283+
71284
```shell
72-
mvn package
285+
mvn install
73286
```
74-
The built jars and javadoc can be found in <strong>target</strong>
75287

76-
Test this project using
77-
```shell
78-
mvn test
288+
The built jars and javadoc can be found in `target`. By using the install Maven goal, the snapshot
289+
version can be used using your local Maven repository for testing purposes, e.g.
290+
291+
```xml
292+
293+
<dependency>
294+
<groupId>org.piwik.java.tracking</groupId>
295+
<artifactId>matomo-java-tracker</artifactId>
296+
<version>2.1-SNAPSHOT</version>
297+
</dependency>
79298
```
80299

81-
This project also supports [Pitest](http://pitest.org/) mutation testing. This report can be generated by calling
300+
This project also supports [Pitest](http://pitest.org/) mutation testing. This report can be generated by calling
301+
82302
```shell
83303
mvn org.pitest:pitest-maven:mutationCoverage
84304
```
85-
and will produce an HTML report at <strong>target/pit-reports/YYYYMMDDHHMI</strong>
305+
306+
and will produce an HTML report at `target/pit-reports/YYYYMMDDHHMI`
86307

87308
Clean this project using
309+
88310
```shell
89311
mvn clean
90312
```
91313

92314
## Contribute
93-
Have a fantastic feature idea? Spot a bug? We would absolutely love for you to contribute to this project! Please feel free to:
315+
316+
Have a fantastic feature idea? Spot a bug? We would absolutely love for you to contribute to this project! Please feel
317+
free to:
94318

95319
* Fork this project
96-
* Create a feature branch from the <strong>master</strong> branch
320+
* Create a feature branch from the _master_ branch
97321
* Write awesome code that does awesome things
98322
* Write awesome test to test your awesome code
99-
* Verify that everything is working as it should by running <strong>mvn test</strong>. If everything passes, you may want to make sure that your tests are covering everything you think they are! Run <strong>mvn org.pitest:pitest-maven:mutationCoverage</strong> to find out!
323+
* Verify that everything is working as it should by running _mvn test_. If everything passes, you may
324+
want to make sure that your tests are covering everything you think they are!
325+
Run `mvn org.pitest:pitest-maven:mutationCoverage` to find out!
100326
* Commit this code to your repository
101327
* Submit a pull request from your branch to our dev branch and let us know why you made the changes you did
102328
* We'll take a look at your request and work to get it integrated with the repo!
103329

104330
## License
331+
105332
This software is released under the BSD 3-Clause license. See [LICENSE](LICENSE).
106333

107334
## Copyright
335+
108336
Copyright (c) 2015 General Electric Company. All rights reserved.

0 commit comments

Comments
 (0)