Skip to content

Commit 5a441a4

Browse files
authored
ga4 (via #1906)
1 parent 7d9e093 commit 5a441a4

File tree

3 files changed

+97
-34
lines changed

3 files changed

+97
-34
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2016-2023 Qameta Software OÜ
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.qameta.allure.ga;
17+
18+
import lombok.Data;
19+
import lombok.experimental.Accessors;
20+
21+
/**
22+
* @author charlie (Dmitry Baev).
23+
*/
24+
@Data
25+
@Accessors(chain = true)
26+
public class GaEvent {
27+
28+
private String name;
29+
private Object params;
30+
31+
}

allure-generator/src/main/java/io/qameta/allure/ga/GaPlugin.java

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.qameta.allure.ga;
1717

18+
import com.fasterxml.jackson.databind.json.JsonMapper;
1819
import io.qameta.allure.Aggregator;
1920
import io.qameta.allure.core.Configuration;
2021
import io.qameta.allure.core.LaunchResults;
@@ -23,12 +24,11 @@
2324
import io.qameta.allure.entity.LabelName;
2425
import org.apache.commons.codec.digest.DigestUtils;
2526
import org.apache.commons.io.IOUtils;
26-
import org.apache.http.NameValuePair;
27-
import org.apache.http.client.entity.UrlEncodedFormEntity;
2827
import org.apache.http.client.methods.HttpPost;
28+
import org.apache.http.entity.ContentType;
29+
import org.apache.http.entity.StringEntity;
2930
import org.apache.http.impl.client.CloseableHttpClient;
3031
import org.apache.http.impl.client.HttpClientBuilder;
31-
import org.apache.http.message.BasicNameValuePair;
3232
import org.slf4j.Logger;
3333
import org.slf4j.LoggerFactory;
3434

@@ -38,8 +38,8 @@
3838
import java.net.UnknownHostException;
3939
import java.nio.charset.StandardCharsets;
4040
import java.nio.file.Path;
41-
import java.util.Arrays;
4241
import java.util.Collection;
42+
import java.util.Collections;
4343
import java.util.List;
4444
import java.util.Objects;
4545
import java.util.Optional;
@@ -60,17 +60,20 @@ public class GaPlugin implements Aggregator {
6060

6161
private static final Logger LOGGER = LoggerFactory.getLogger(GaPlugin.class);
6262

63-
private static final String LOCAL = "Local";
63+
private static final String LOCAL = "local";
6464

6565
private static final String UNDEFINED = "Undefined";
6666

6767
private static final String GA_DISABLE = "ALLURE_NO_ANALYTICS";
6868

69-
private static final String GA_ID = "UA-88115679-3";
70-
private static final String GA_ENDPOINT = "https://www.google-analytics.com/collect";
71-
private static final String GA_API_VERSION = "1";
69+
private static final String GA_ENDPOINT_FORMAT
70+
= "https://www.google-analytics.com/mp/collect?measurement_id=%s&api_secret=%s";
71+
72+
private static final String MEASUREMENT_ID = "G-FVWC4GKEYS";
73+
private static final String GA_SECRET = "rboZz0HySdmCVIvtydmSTQ";
7274

7375
private static final String ALLURE_VERSION_TXT_PATH = "/allure-version.txt";
76+
private static final String GA_EVENT_NAME = "report_generated";
7477

7578
@Override
7679
public void aggregate(final Configuration configuration,
@@ -103,27 +106,23 @@ public void aggregate(final Configuration configuration,
103106
protected void sendStats(final String clientId, final GaParameters parameters) {
104107
final HttpClientBuilder builder = HttpClientBuilder.create();
105108
try (CloseableHttpClient client = builder.build()) {
106-
final List<NameValuePair> pairs = Arrays.asList(
107-
pair("v", GA_API_VERSION),
108-
pair("aip", GA_API_VERSION),
109-
pair("tid", GA_ID),
110-
pair("z", UUID.randomUUID().toString()),
111-
pair("sc", "end"),
112-
pair("t", "event"),
113-
pair("cid", clientId),
114-
pair("an", "Allure Report"),
115-
pair("ec", "Allure CLI events"),
116-
pair("ea", "Report generate"),
117-
pair("av", parameters.getAllureVersion()),
118-
pair("ds", "Report generator"),
119-
pair("cd6", parameters.getLanguage()),
120-
pair("cd5", parameters.getFramework()),
121-
pair("cd2", parameters.getExecutorType()),
122-
pair("cd4", parameters.getResultsFormat()),
123-
pair("cm1", String.valueOf(parameters.getResultsCount()))
109+
final String uri = String.format(
110+
GA_ENDPOINT_FORMAT,
111+
MEASUREMENT_ID, GA_SECRET
112+
);
113+
final HttpPost post = new HttpPost(uri);
114+
final String stringBody = new JsonMapper().writeValueAsString(
115+
new GaRequest()
116+
.setClientId(clientId)
117+
.setEvents(Collections.singletonList(new GaEvent()
118+
.setName(GA_EVENT_NAME)
119+
.setParams(parameters)
120+
))
121+
);
122+
final StringEntity entity = new StringEntity(
123+
stringBody,
124+
ContentType.APPLICATION_JSON.withCharset(StandardCharsets.UTF_8)
124125
);
125-
final HttpPost post = new HttpPost(GA_ENDPOINT);
126-
final UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, StandardCharsets.UTF_8);
127126
post.setEntity(entity);
128127
client.execute(post).close();
129128
LOGGER.debug("GA done");
@@ -175,8 +174,10 @@ private static String getExecutorType(final List<LaunchResults> launchesResults)
175174
.map(results -> results.<ExecutorInfo>getExtra(EXECUTORS_BLOCK_NAME))
176175
.filter(Optional::isPresent)
177176
.map(Optional::get)
178-
.findFirst()
179177
.map(ExecutorInfo::getType)
178+
.map(String::trim)
179+
.map(String::toLowerCase)
180+
.findFirst()
180181
.orElse(LOCAL);
181182
}
182183

@@ -205,13 +206,9 @@ private static Optional<String> getLocalHostName() {
205206
try {
206207
return Optional.ofNullable(InetAddress.getLocalHost().getHostName());
207208
} catch (UnknownHostException e) {
208-
LOGGER.debug("Could not get host name {}", e);
209+
LOGGER.debug("Could not get host name", e);
209210
return Optional.empty();
210211
}
211212
}
212213

213-
private static NameValuePair pair(final String v, final String value) {
214-
return new BasicNameValuePair(v, value);
215-
}
216-
217214
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2016-2023 Qameta Software OÜ
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.qameta.allure.ga;
17+
18+
import com.fasterxml.jackson.annotation.JsonProperty;
19+
import lombok.Data;
20+
import lombok.experimental.Accessors;
21+
22+
import java.util.List;
23+
24+
/**
25+
* @author charlie (Dmitry Baev).
26+
*/
27+
@Data
28+
@Accessors(chain = true)
29+
public class GaRequest {
30+
31+
@JsonProperty("client_id")
32+
private String clientId;
33+
private List<GaEvent> events;
34+
35+
}

0 commit comments

Comments
 (0)