Skip to content

Commit

Permalink
ga4 (via #1906)
Browse files Browse the repository at this point in the history
  • Loading branch information
baev authored Feb 2, 2023
1 parent 7d9e093 commit 5a441a4
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 34 deletions.
31 changes: 31 additions & 0 deletions allure-generator/src/main/java/io/qameta/allure/ga/GaEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2016-2023 Qameta Software OÜ
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.qameta.allure.ga;

import lombok.Data;
import lombok.experimental.Accessors;

/**
* @author charlie (Dmitry Baev).
*/
@Data
@Accessors(chain = true)
public class GaEvent {

private String name;
private Object params;

}
65 changes: 31 additions & 34 deletions allure-generator/src/main/java/io/qameta/allure/ga/GaPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.qameta.allure.ga;

import com.fasterxml.jackson.databind.json.JsonMapper;
import io.qameta.allure.Aggregator;
import io.qameta.allure.core.Configuration;
import io.qameta.allure.core.LaunchResults;
Expand All @@ -23,12 +24,11 @@
import io.qameta.allure.entity.LabelName;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -38,8 +38,8 @@
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -60,17 +60,20 @@ public class GaPlugin implements Aggregator {

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

private static final String LOCAL = "Local";
private static final String LOCAL = "local";

private static final String UNDEFINED = "Undefined";

private static final String GA_DISABLE = "ALLURE_NO_ANALYTICS";

private static final String GA_ID = "UA-88115679-3";
private static final String GA_ENDPOINT = "https://www.google-analytics.com/collect";
private static final String GA_API_VERSION = "1";
private static final String GA_ENDPOINT_FORMAT
= "https://www.google-analytics.com/mp/collect?measurement_id=%s&api_secret=%s";

private static final String MEASUREMENT_ID = "G-FVWC4GKEYS";
private static final String GA_SECRET = "rboZz0HySdmCVIvtydmSTQ";

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

@Override
public void aggregate(final Configuration configuration,
Expand Down Expand Up @@ -103,27 +106,23 @@ public void aggregate(final Configuration configuration,
protected void sendStats(final String clientId, final GaParameters parameters) {
final HttpClientBuilder builder = HttpClientBuilder.create();
try (CloseableHttpClient client = builder.build()) {
final List<NameValuePair> pairs = Arrays.asList(
pair("v", GA_API_VERSION),
pair("aip", GA_API_VERSION),
pair("tid", GA_ID),
pair("z", UUID.randomUUID().toString()),
pair("sc", "end"),
pair("t", "event"),
pair("cid", clientId),
pair("an", "Allure Report"),
pair("ec", "Allure CLI events"),
pair("ea", "Report generate"),
pair("av", parameters.getAllureVersion()),
pair("ds", "Report generator"),
pair("cd6", parameters.getLanguage()),
pair("cd5", parameters.getFramework()),
pair("cd2", parameters.getExecutorType()),
pair("cd4", parameters.getResultsFormat()),
pair("cm1", String.valueOf(parameters.getResultsCount()))
final String uri = String.format(
GA_ENDPOINT_FORMAT,
MEASUREMENT_ID, GA_SECRET
);
final HttpPost post = new HttpPost(uri);
final String stringBody = new JsonMapper().writeValueAsString(
new GaRequest()
.setClientId(clientId)
.setEvents(Collections.singletonList(new GaEvent()
.setName(GA_EVENT_NAME)
.setParams(parameters)
))
);
final StringEntity entity = new StringEntity(
stringBody,
ContentType.APPLICATION_JSON.withCharset(StandardCharsets.UTF_8)
);
final HttpPost post = new HttpPost(GA_ENDPOINT);
final UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, StandardCharsets.UTF_8);
post.setEntity(entity);
client.execute(post).close();
LOGGER.debug("GA done");
Expand Down Expand Up @@ -175,8 +174,10 @@ private static String getExecutorType(final List<LaunchResults> launchesResults)
.map(results -> results.<ExecutorInfo>getExtra(EXECUTORS_BLOCK_NAME))
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst()
.map(ExecutorInfo::getType)
.map(String::trim)
.map(String::toLowerCase)
.findFirst()
.orElse(LOCAL);
}

Expand Down Expand Up @@ -205,13 +206,9 @@ private static Optional<String> getLocalHostName() {
try {
return Optional.ofNullable(InetAddress.getLocalHost().getHostName());
} catch (UnknownHostException e) {
LOGGER.debug("Could not get host name {}", e);
LOGGER.debug("Could not get host name", e);
return Optional.empty();
}
}

private static NameValuePair pair(final String v, final String value) {
return new BasicNameValuePair(v, value);
}

}
35 changes: 35 additions & 0 deletions allure-generator/src/main/java/io/qameta/allure/ga/GaRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2016-2023 Qameta Software OÜ
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.qameta.allure.ga;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.List;

/**
* @author charlie (Dmitry Baev).
*/
@Data
@Accessors(chain = true)
public class GaRequest {

@JsonProperty("client_id")
private String clientId;
private List<GaEvent> events;

}

0 comments on commit 5a441a4

Please sign in to comment.