1515 */
1616package io .qameta .allure .ga ;
1717
18+ import com .fasterxml .jackson .databind .json .JsonMapper ;
1819import io .qameta .allure .Aggregator ;
1920import io .qameta .allure .core .Configuration ;
2021import io .qameta .allure .core .LaunchResults ;
2324import io .qameta .allure .entity .LabelName ;
2425import org .apache .commons .codec .digest .DigestUtils ;
2526import org .apache .commons .io .IOUtils ;
26- import org .apache .http .NameValuePair ;
27- import org .apache .http .client .entity .UrlEncodedFormEntity ;
2827import org .apache .http .client .methods .HttpPost ;
28+ import org .apache .http .entity .ContentType ;
29+ import org .apache .http .entity .StringEntity ;
2930import org .apache .http .impl .client .CloseableHttpClient ;
3031import org .apache .http .impl .client .HttpClientBuilder ;
31- import org .apache .http .message .BasicNameValuePair ;
3232import org .slf4j .Logger ;
3333import org .slf4j .LoggerFactory ;
3434
3838import java .net .UnknownHostException ;
3939import java .nio .charset .StandardCharsets ;
4040import java .nio .file .Path ;
41- import java .util .Arrays ;
4241import java .util .Collection ;
42+ import java .util .Collections ;
4343import java .util .List ;
4444import java .util .Objects ;
4545import 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}
0 commit comments