Skip to content

Commit 9ed5ebd

Browse files
committed
added google analytics
1 parent d59eeb5 commit 9ed5ebd

File tree

14 files changed

+251
-46
lines changed

14 files changed

+251
-46
lines changed

src/com/ss/editor/JFXApplication.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ public void start(final Stage stage) throws Exception {
153153

154154
public void onExit() {
155155

156-
GAnalytics.sendEvent(GAEvent.Category.EDITOR,
157-
GAEvent.Action.EDITOR_CLOSED, GAEvent.Label.THE_EDITOR_WAS_CLOSED);
156+
GAnalytics.sendEvent(GAEvent.Category.APPLICATION,
157+
GAEvent.Action.CLOSED, GAEvent.Label.THE_EDITOR_APP_WAS_CLOSED);
158158

159159
final EditorConfig config = EditorConfig.getInstance();
160160
config.save();
@@ -181,8 +181,8 @@ public void buildScene() {
181181

182182
JMEFilePreviewManager.getInstance();
183183

184-
GAnalytics.sendEvent(GAEvent.Category.EDITOR,
185-
GAEvent.Action.EDITOR_LAUNCHED, GAEvent.Label.THE_EDITOR_WAS_LAUNCHED);
184+
GAnalytics.sendEvent(GAEvent.Category.APPLICATION,
185+
GAEvent.Action.LAUNCHED, GAEvent.Label.THE_EDITOR_APP_WAS_LAUNCHED);
186186
}
187187

188188
/**

src/com/ss/editor/analytics/google/GAEvent.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,28 @@
66
public interface GAEvent {
77

88
interface Category {
9+
String APPLICATION = "Application";
10+
String DIALOG = "Dialog";
911
String EDITOR = "Editor";
1012
}
1113

1214
interface Action {
13-
String EDITOR_LAUNCHED = "Launched";
15+
String LAUNCHED = "Launched";
16+
String CLOSED = "Closed";
17+
String EDITOR_OPENED = "Opened";
1418
String EDITOR_CLOSED = "Closed";
15-
String OPEN = "Open";
16-
String CLOSE = "Close";
19+
String DIALOG_OPENED = "Opened";
20+
String DIALOG_CLOSED = "Closed";
1721
}
1822

1923
interface Label {
24+
String THE_EDITOR_APP_WAS_CLOSED = "The editor application was closed";
25+
String THE_EDITOR_APP_WAS_LAUNCHED = "The editor application was launched";
26+
String THE_EDITOR_WAS_OPENED = "The editor was opened for editing a file";
2027
String THE_EDITOR_WAS_CLOSED = "The editor was closed";
21-
String THE_EDITOR_WAS_LAUNCHED = "The editor was launched";
22-
String EXPORT_DESTINATION_CHOOSER = "Export destination chooser";
28+
String THE_DIALOG_WAS_OPENED = "The dialog was opened";
29+
String THE_DIALOG_WAS_CLOSED = "The dialog was closed";
30+
String WORKING_ON_AN_EDITOR = "Working on an editor";
31+
String SHOWING_A_DIALOG = "Showing a dialog";
2332
}
2433
}

src/com/ss/editor/analytics/google/GAnalytics.java

Lines changed: 108 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static rlib.util.StringUtils.isEmpty;
55

66
import com.ss.editor.EditorThread;
7+
import com.ss.editor.config.Config;
78

89
import org.apache.http.StatusLine;
910
import org.apache.http.client.methods.CloseableHttpResponse;
@@ -19,15 +20,18 @@
1920
import java.io.UnsupportedEncodingException;
2021
import java.net.URLEncoder;
2122
import java.util.HashMap;
23+
import java.util.Locale;
2224
import java.util.Map;
2325
import java.util.concurrent.atomic.AtomicInteger;
2426

2527
import rlib.concurrent.util.ConcurrentUtils;
2628
import rlib.logging.Logger;
2729
import rlib.logging.LoggerManager;
2830
import rlib.util.StringUtils;
31+
import rlib.util.Util;
2932
import rlib.util.linkedlist.LinkedList;
3033
import rlib.util.linkedlist.LinkedListFactory;
34+
import rlib.util.os.OperatingSystem;
3135

3236
/**
3337
* @author JavaSaBr
@@ -40,18 +44,16 @@ public class GAnalytics extends EditorThread {
4044
public static final String PARAM_TRACKING_ID = "tid";
4145
public static final String PARAM_CLIENT_ID = "cid";
4246
public static final String PARAM_HIT_TYPE = "t";
43-
public static final String PARAM_DATA_SOURCE = "ds";
44-
public static final String PARAM_QUEUE_TIME = "qt";
45-
public static final String PARAM_CACHE_BUSTER = "z";
4647
public static final String PARAM_USER_ID = "uid";
47-
public static final String PARAM_SESSION_CONTROL = "sc";
48-
public static final String PARAM_SCREEN_RESOLUTION = "sr";
4948
public static final String PARAM_USER_LANGUAGE = "ul";
5049
public static final String PARAM_APPLICATION_VERSION = "av";
5150
public static final String PARAM_EVENT_CATEGORY = "ec";
5251
public static final String PARAM_EVENT_ACTION = "ea";
5352
public static final String PARAM_EVENT_LABEL = "el";
5453
public static final String PARAM_EVENT_VALUE = "ev";
54+
public static final String PARAM_PAGE_VIEW_LOCATION = "dl";
55+
public static final String PARAM_PAGE_VIEW_TITLE = "dt";
56+
public static final String PARAM_PAGE_VIEW_PAGE = "dp";
5557
public static final String PARAM_USER_TIMING_CATEGORY = "utc";
5658
public static final String PARAM_USER_TIMING_VAR_NAME = "utv";
5759
public static final String PARAM_USER_TIMING_TIME = "utt";
@@ -61,7 +63,6 @@ public class GAnalytics extends EditorThread {
6163
public static final String PARAM_CUSTOM_DIMENSION = "cd";
6264

6365
public static final String FIELD_OS = PARAM_CUSTOM_DIMENSION + "1";
64-
public static final String FIELD_GRAPHICS_ADAPTER = PARAM_CUSTOM_DIMENSION + "2";
6566

6667
public static final String PROP_ANALYTICS_HOST = "http://www.google-analytics.com/collect";
6768
public static final String PROP_TRACKING_ID = "UA-89459340-1";
@@ -73,29 +74,53 @@ public static GAnalytics getInstance() {
7374
return INSTANCE;
7475
}
7576

77+
/**
78+
* Wait for sending max 2 sec.
79+
*/
7680
public static void waitForSend() {
7781
final GAnalytics instance = getInstance();
7882
final AtomicInteger progressCount = instance.progressCount;
79-
if(progressCount.get() < 1) return;
83+
if (progressCount.get() < 1) return;
8084
ConcurrentUtils.wait(progressCount, 2000);
8185
}
8286

87+
/**
88+
* Send an event.
89+
*
90+
* @param category the category.
91+
* @param action the action.
92+
*/
8393
public static void sendEvent(@NotNull final String category, @NotNull final String action) {
8494
sendEvent(category, action, null, null);
8595
}
8696

97+
/**
98+
* Send an event.
99+
*
100+
* @param category the category.
101+
* @param action the action.
102+
* @param label the label.
103+
*/
87104
public static void sendEvent(@NotNull final String category, @NotNull final String action, @Nullable final String label) {
88105
sendEvent(category, action, label, null);
89106
}
90107

108+
/**
109+
* Send an event.
110+
*
111+
* @param category the category.
112+
* @param action the action.
113+
* @param label the label.
114+
* @param value the value.
115+
*/
91116
public static void sendEvent(@NotNull final String category, @NotNull final String action, @Nullable final String label, @Nullable final String value) {
92117

93118
final Map<String, Object> parameters = new HashMap<>();
94119
parameters.put(PARAM_EVENT_CATEGORY, category);
95120
parameters.put(PARAM_EVENT_ACTION, action);
96121

97-
if(!isEmpty(label)) parameters.put(PARAM_EVENT_LABEL, label);
98-
if(!isEmpty(value)) parameters.put(PARAM_EVENT_VALUE, value);
122+
if (!isEmpty(label)) parameters.put(PARAM_EVENT_LABEL, label);
123+
if (!isEmpty(value)) parameters.put(PARAM_EVENT_VALUE, value);
99124

100125
send(HitType.EVENT, parameters);
101126
}
@@ -104,7 +129,7 @@ public static void sendEvent(@NotNull final String category, @NotNull final Stri
104129
* Send an exception.
105130
*
106131
* @param exception the exception.
107-
* @param fatal true if the exception is fatal.
132+
* @param fatal true if the exception is fatal.
108133
*/
109134
public static void sendException(@NotNull final Throwable exception, final boolean fatal) {
110135

@@ -123,33 +148,93 @@ public static void sendException(@NotNull final Throwable exception, final boole
123148
send(HitType.EXCEPTION, parameters);
124149
}
125150

151+
/**
152+
* Send a page view event.
153+
*
154+
* @param title the title.
155+
* @param location the location.
156+
* @param page the page.
157+
*/
158+
public static void sendPageView(@Nullable final String title, @Nullable final String location,
159+
@Nullable final String page) {
160+
161+
final Map<String, Object> parameters = new HashMap<>();
162+
if (!isEmpty(title)) parameters.put(PARAM_PAGE_VIEW_TITLE, title);
163+
if (!isEmpty(location)) parameters.put(PARAM_PAGE_VIEW_LOCATION, location);
164+
if (!isEmpty(page)) parameters.put(PARAM_PAGE_VIEW_PAGE, page);
165+
166+
send(HitType.PAGE_VIEW, parameters);
167+
}
168+
169+
/**
170+
* Send a timing stats.
171+
*
172+
* @param timingCategory the category.
173+
* @param timingVar the variable.
174+
* @param timingValue the value.
175+
* @param timingLabel the label.
176+
*/
177+
public static void sendTiming(@NotNull final String timingCategory, @NotNull final String timingVar,
178+
final int timingValue, @Nullable final String timingLabel) {
179+
180+
final Map<String, Object> parameters = new HashMap<>();
181+
parameters.put(PARAM_USER_TIMING_CATEGORY, timingCategory);
182+
parameters.put(PARAM_USER_TIMING_VAR_NAME, timingVar);
183+
parameters.put(PARAM_USER_TIMING_TIME, timingValue);
184+
185+
if (!isEmpty(timingLabel)) parameters.put(PARAM_USER_TIMING_LABEL, timingLabel);
186+
187+
send(HitType.TIMING, parameters);
188+
}
189+
190+
/**
191+
* Send an analytic event.
192+
*
193+
* @param hitType the hit type.
194+
* @param parameters the parameters.
195+
*/
126196
private static void send(final HitType hitType, final Map<String, Object> parameters) {
127197
parameters.put(PARAM_HIT_TYPE, hitType.toString());
128198
send(parameters);
129199
}
130200

201+
/**
202+
* Send an analytic event.
203+
*
204+
* @param parameters the parameters.
205+
*/
131206
private static void send(final Map<String, Object> parameters) {
132207
getInstance().addTask(() -> doSend(parameters));
133208
}
134209

210+
/**
211+
* Process sending an analytic events.
212+
*
213+
* @param parameters the parameters.
214+
*/
135215
private static void doSend(final Map<String, Object> parameters) {
136216

137-
String os = null;
138-
String graphicsAdapter = null;
217+
final OperatingSystem operatingSystem = Config.OPERATING_SYSTEM;
218+
219+
final String distribution = operatingSystem.getDistribution();
220+
final String os = StringUtils.isEmpty(distribution) ? operatingSystem.getName() + " " + operatingSystem.getVersion() : distribution;
221+
final String appVersion = Config.VERSION;
222+
final String language = Locale.getDefault().toString();
223+
final String userId = Util.getUserName();
139224

140225
final GAnalytics instance = getInstance();
141226
final AtomicInteger progressCount = instance.progressCount;
142227

143-
System.out.println("start sending " + parameters);
144-
145-
try(final CloseableHttpClient httpClient = createMinimal()) {
228+
try (final CloseableHttpClient httpClient = createMinimal()) {
146229

147230
parameters.put(PARAM_PROTOCOL_VERSION, "1");
148231
parameters.put(PARAM_TRACKING_ID, PROP_TRACKING_ID);
149232
parameters.put(PARAM_CLIENT_ID, PROP_CLIENT_ID);
150233

151-
if(!StringUtils.isEmpty(os)) parameters.put(FIELD_OS, os);
152-
if(!StringUtils.isEmpty(graphicsAdapter)) parameters.put(FIELD_GRAPHICS_ADAPTER, graphicsAdapter);
234+
if (!StringUtils.isEmpty(os)) parameters.put(FIELD_OS, os);
235+
if (!StringUtils.isEmpty(appVersion)) parameters.put(PARAM_APPLICATION_VERSION, appVersion);
236+
if (!StringUtils.isEmpty(language)) parameters.put(PARAM_USER_LANGUAGE, language);
237+
if (!StringUtils.isEmpty(userId)) parameters.put(PARAM_USER_ID, userId);
153238

154239
final String stringParameters = buildParameters(parameters);
155240
final byte[] byteParameters = stringParameters.getBytes("UTF-8");
@@ -164,8 +249,6 @@ private static void doSend(final Map<String, Object> parameters) {
164249
LOGGER.warning("failed analytics request: " + response);
165250
}
166251

167-
System.out.println("finished sending.");
168-
169252
} catch (final IOException e) {
170253
LOGGER.warning(e);
171254
} finally {
@@ -175,23 +258,20 @@ private static void doSend(final Map<String, Object> parameters) {
175258
}
176259

177260
private static String buildParameters(final Map<String, Object> parameters) {
178-
179261
final StringBuilder builder = new StringBuilder();
180-
181262
parameters.forEach((key, value) -> appendParam(builder, key, value));
182-
183263
return builder.toString();
184264
}
185265

186266
private static void appendParam(final StringBuilder builder, final String key, final Object value) {
187-
if(value == null) return;
188-
else if(builder.length() > 1) {
267+
if (value == null) return;
268+
else if (builder.length() > 1) {
189269
builder.append('&');
190270
}
191271

192272
builder.append(key).append('=');
193273

194-
if(value instanceof String) {
274+
if (value instanceof String) {
195275
try {
196276
builder.append(URLEncoder.encode((String) value, "UTF-8"));
197277
} catch (final UnsupportedEncodingException e) {
@@ -233,13 +313,13 @@ private void addTask(@NotNull final Runnable runnable) {
233313

234314
@Override
235315
public void run() {
236-
for(;;) {
316+
for (; ; ) {
237317

238-
Runnable next = null;
318+
Runnable next;
239319

240320
synchronized (queue) {
241321
next = queue.pollFirst();
242-
if(next == null) {
322+
if (next == null) {
243323
ConcurrentUtils.waitInSynchronize(queue);
244324
continue;
245325
}

src/com/ss/editor/config/Config.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66

77
import org.jetbrains.annotations.NotNull;
88

9+
import java.awt.GraphicsDevice;
10+
import java.awt.GraphicsEnvironment;
911
import java.nio.file.Path;
1012
import java.nio.file.Paths;
1113

1214
import rlib.util.Util;
1315
import rlib.util.VarTable;
16+
import rlib.util.os.OperatingSystem;
1417

1518
/**
1619
* The configuration of this application.
@@ -31,6 +34,16 @@ public abstract class Config {
3134
*/
3235
public static final String PROJECT_PATH;
3336

37+
/**
38+
* The graphics adapter.
39+
*/
40+
public static final GraphicsDevice GRAPHICS_DEVICE;
41+
42+
/**
43+
* The operation system.
44+
*/
45+
public static final OperatingSystem OPERATING_SYSTEM;
46+
3447
/**
3548
* Flag is for showing debug.
3649
*/
@@ -48,12 +61,18 @@ public abstract class Config {
4861

4962
static {
5063

64+
final GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
65+
final GraphicsDevice device = graphicsEnvironment.getDefaultScreenDevice();
66+
5167
final VarTable vars = new DocumentConfig(EditorUtil.getInputStream(CONFIG_RESOURCE_PATH)).parse();
5268

5369
DEV_DEBUG = vars.getBoolean("Dev.debug", false);
5470
DEV_DEBUG_JFX = vars.getBoolean("Dev.debugJFX", false);
5571
ENABLE_PBR = vars.getBoolean("Graphics.enablePBR", true);
5672

73+
GRAPHICS_DEVICE = device;
74+
OPERATING_SYSTEM = new OperatingSystem();
75+
5776
PROJECT_PATH = Util.getRootFolderFromClass(Editor.class).toString();
5877
}
5978

0 commit comments

Comments
 (0)