Skip to content

Commit

Permalink
Add version info to Google Analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
herrtunante committed Jun 30, 2023
1 parent 258147e commit 44ed53a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 82 deletions.
Expand Up @@ -6,6 +6,7 @@
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
Expand All @@ -17,6 +18,7 @@
import java.sql.DriverManager;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down
Expand Up @@ -7,9 +7,9 @@
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.openforis.collect.earth.app.service.UpdateIniUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -26,7 +26,6 @@ public static void logGAnalytics(String event) {
public void run() {
try( CloseableHttpClient httpclient = HttpClients.createDefault() ) {
// Following instruction from https://developers.google.com/analytics/devguides/collection/protocol/ga4/sending-events?hl=en&client_type=gtag
HttpPost httppost = new HttpPost();
// See https://ga-dev-tools.google/ga4/event-builder/?p=2&d=0&f=1&c=custom_event&j=PlotSaved&n=CollectEarth&k=E11gVKqxSamFWwCswJPKIQ&i=G-8K943HZKJZ&e=1687947214291000&m=W10&b=W1tdXQ
// Request parameters and other properties.

Expand All @@ -42,11 +41,15 @@ public void run() {
+ "\"non_personalized_ads\":true,"
+ "\"events\":["
+ "{"
+ "\"name\":\"" + event + "\""
+ "\"name\":\"" + event + "\","
+ "\"params\":{"
+ "\"items\": [],"
+ "\"version\": \""+ UpdateIniUtils.getVersionInstalled()+"\""
+ "}"
+ "}"
+ "]"
+ "}";

try(OutputStream os = con.getOutputStream()) {
byte[] input = jsonToSend.getBytes("utf-8");
os.write(input, 0, input.length);
Expand All @@ -60,7 +63,7 @@ public void run() {
System.out.println(response.toString());
}

logger.info(event + " GA Logged - Response http " + con.getResponseCode());
logger.info(event + " GA Logged - Response http " + con.getResponseCode() + "/r/n" + jsonToSend);
} catch (IOException e) {
logger.error("Error generating URL for Analytics", e);
}
Expand Down
Expand Up @@ -195,4 +195,9 @@ public String convertToDate(String buildVersionNumber) {

return reformattedStr;
}

public String getBuildDate() {
return convertToDate( getReleaseNameInstalled() );
}

}
Expand Up @@ -3,12 +3,8 @@
import java.awt.Desktop;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;

import javax.swing.Box;
import javax.swing.JButton;
Expand All @@ -29,102 +25,81 @@ public class AboutDialog extends JDialog {

public AboutDialog(JFrame parent, String title) {
super(parent, title, true);

UpdateIniUtils updateIniUtils = new UpdateIniUtils();
String buildDate = updateIniUtils.convertToDate(getBuild());

Box b = Box.createVerticalBox();
b.setAlignmentX(CENTER_ALIGNMENT);
b.setBorder(new EmptyBorder(10, 10, 10, 10));
b.add(Box.createGlue());
b.add(new JLabel("Collect Earth v. " + getVersion() + " ( built " + buildDate + ") - JRE v. " + System.getProperty("java.version"))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
b.add(new JLabel("By Open Foris Initiative / Part of the Food and Agriculture Organization of the UN")); //$NON-NLS-1$
JLabel comp = new JLabel("<html>" + Messages.getString("AboutDialog.5") + "</html>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
JLabel comp2 = new JLabel("<html><a href='https://github.com/openforis/collect-earth/blob/master/collect-earth/CHANGELOG.md'>CHECK THE CHANGE LOG</a></html>");
if (isBrowsingSupported()) {
makeLinkable(comp, new LinkMouseListener( "http://www.openforis.org" ));
makeLinkable(comp2, new LinkMouseListener( "https://github.com/openforis/collect-earth/blob/master/collect-earth/CHANGELOG.md" ));
}
b.add(comp);
b.add(comp2);
b.add(Box.createGlue());
getContentPane().add(b, "Center"); //$NON-NLS-1$

JPanel p2 = new JPanel();
JButton ok = new JButton(Messages.getString("AboutDialog.8")); //$NON-NLS-1$
p2.add(ok);
getContentPane().add(p2, "South"); //$NON-NLS-1$

ok.addActionListener( e -> setVisible(false) );

setSize(380, 150);
}

private String getBuild() {
String key = "version_id"; //$NON-NLS-1$
return getValueFromUpdateIni(key);
}

public String getValueFromUpdateIni(String key) {
Properties properties = new Properties();
String value = "unknwown"; //$NON-NLS-1$
try (FileInputStream fis = new FileInputStream("update.ini")) { //$NON-NLS-1$
properties.load(fis);
value = properties.getProperty(key);
} catch (FileNotFoundException e) {
logger.error("The update.,ini file could not be found", e); //$NON-NLS-1$
} catch (IOException e) {
logger.error("Error opening the update.ini file", e); //$NON-NLS-1$
try {
UpdateIniUtils updateIniUtils = new UpdateIniUtils();
String buildDate = updateIniUtils.getBuildDate();

Box b = Box.createVerticalBox();
b.setAlignmentX(CENTER_ALIGNMENT);
b.setBorder(new EmptyBorder(10, 10, 10, 10));
b.add(Box.createGlue());
b.add(new JLabel("Collect Earth v. " + UpdateIniUtils.getVersionInstalled() + " ( built " + buildDate + ") - JRE v. " + System.getProperty("java.version"))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
b.add(new JLabel("By Open Foris Initiative / Part of the Food and Agriculture Organization of the UN")); //$NON-NLS-1$
JLabel comp = new JLabel("<html>" + Messages.getString("AboutDialog.5") + "</html>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
JLabel comp2 = new JLabel("<html><a href='https://github.com/openforis/collect-earth/blob/master/collect-earth/CHANGELOG.md'>CHECK THE CHANGE LOG</a></html>");
if (isBrowsingSupported()) {
makeLinkable(comp, new LinkMouseListener( "http://www.openforis.org" ));
makeLinkable(comp2, new LinkMouseListener( "https://github.com/openforis/collect-earth/blob/master/collect-earth/CHANGELOG.md" ));
}
b.add(comp);
b.add(comp2);
b.add(Box.createGlue());
getContentPane().add(b, "Center"); //$NON-NLS-1$

JPanel p2 = new JPanel();
JButton ok = new JButton(Messages.getString("AboutDialog.8")); //$NON-NLS-1$
p2.add(ok);
getContentPane().add(p2, "South"); //$NON-NLS-1$

ok.addActionListener( e -> setVisible(false) );

setSize(380, 150);
}catch(Exception e) {
logger.error( "Error showing About dialog", e );
}
return value;
}

private String getVersion() {
String key = "version"; //$NON-NLS-1$
return getValueFromUpdateIni(key);
}

private static void makeLinkable(JLabel c, MouseListener ml) {
assert ml != null;
assert ml != null;

c.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
c.addMouseListener(ml);
c.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
c.addMouseListener(ml);
}

private static boolean isBrowsingSupported() {
if (!Desktop.isDesktopSupported()) {
return false;
}
boolean result = false;
Desktop desktop = java.awt.Desktop.getDesktop();
if (desktop.isSupported(Desktop.Action.BROWSE)) {
result = true;
}
return result;
if (!Desktop.isDesktopSupported()) {
return false;
}
boolean result = false;
Desktop desktop = java.awt.Desktop.getDesktop();
if (desktop.isSupported(Desktop.Action.BROWSE)) {
result = true;
}
return result;

}

private static class LinkMouseListener extends MouseAdapter {
String url;


public LinkMouseListener(String url) {
public LinkMouseListener(String url) {
super();
this.url = url;
}


@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
JLabel l = (JLabel) evt.getSource();
try {
public void mouseClicked(java.awt.event.MouseEvent evt) {
JLabel l = (JLabel) evt.getSource();
try {

URI uri = new java.net.URI(url); //$NON-NLS-1$
(new LinkRunner(uri)).execute();
} catch (URISyntaxException use) {
throw new AssertionError(use + ": " + l.getText()); //NOI18N //$NON-NLS-1$
}
}
(new LinkRunner(uri)).execute();
} catch (URISyntaxException use) {
throw new AssertionError(use + ": " + l.getText()); //NOI18N //$NON-NLS-1$
}
}
}

}

0 comments on commit 44ed53a

Please sign in to comment.