* fixed page view tracking and reset session info before initializing tracking
This commit is contained in:
parent
caef14cfa4
commit
6c415e4113
|
@ -20,18 +20,36 @@ import com.sun.jna.Platform;
|
||||||
|
|
||||||
public class Analytics {
|
public class Analytics {
|
||||||
|
|
||||||
private static final Map<String, String> persistentData = Settings.forPackage(Analytics.class).node("analytics").asMap();
|
private static JGoogleAnalyticsTracker tracker;
|
||||||
private static final String VISITOR_ID = "visitorId";
|
private static VisitorData visitorData;
|
||||||
private static final String TIMESTAMP_FIRST = "timestampFirst";
|
|
||||||
private static final String TIMESTAMP_LAST = "timestampLast";
|
|
||||||
private static final String VISITS = "visits";
|
|
||||||
|
|
||||||
private static final VisitorData visitorData = restoreVisitorData();
|
private static String host = "filebot.sourceforge.net";
|
||||||
private static final JGoogleAnalyticsTracker tracker = new JGoogleAnalyticsTracker(getConfig(getApplicationProperty("analytics.WebPropertyID"), visitorData), V_4_7_2);
|
|
||||||
|
|
||||||
private static final String host = "filebot.sourceforge.net";
|
|
||||||
private static String currentView = null;
|
private static String currentView = null;
|
||||||
|
|
||||||
|
private static boolean enabled = false;
|
||||||
|
|
||||||
|
|
||||||
|
public static synchronized JGoogleAnalyticsTracker getTracker() {
|
||||||
|
if (tracker != null)
|
||||||
|
return tracker;
|
||||||
|
|
||||||
|
// initialize tracker
|
||||||
|
visitorData = restoreVisitorData();
|
||||||
|
tracker = new JGoogleAnalyticsTracker(getConfig(getApplicationProperty("analytics.WebPropertyID"), visitorData), V_4_7_2);
|
||||||
|
|
||||||
|
// store session data on shutdown
|
||||||
|
Runtime.getRuntime().addShutdownHook(new Thread("AnalyticsShutdownHook") {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
storeVisitorData(visitorData);
|
||||||
|
JGoogleAnalyticsTracker.completeBackgroundTasks(2000);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return tracker;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void trackView(Class<?> view, String title) {
|
public static void trackView(Class<?> view, String title) {
|
||||||
trackView(view.getName().replace('.', '/'), title);
|
trackView(view.getName().replace('.', '/'), title);
|
||||||
|
@ -39,15 +57,15 @@ public class Analytics {
|
||||||
|
|
||||||
|
|
||||||
public static synchronized void trackView(String view, String title) {
|
public static synchronized void trackView(String view, String title) {
|
||||||
if (!tracker.isEnabled())
|
if (!enabled)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (currentView == null) {
|
if (currentView == null) {
|
||||||
// track application startup
|
// track application startup
|
||||||
tracker.trackPageViewFromSearch(view, title, host, getJavaVersionIdentifier(), getDeploymentMethod());
|
getTracker().trackPageViewFromSearch(view, title, host, getJavaVersionIdentifier(), getDeploymentMethod());
|
||||||
} else {
|
} else {
|
||||||
// track application state change
|
// track application state change
|
||||||
tracker.trackPageViewFromReferrer(view, title, host, host, currentView);
|
getTracker().trackPageViewFromReferrer(view, title, host, host, currentView);
|
||||||
}
|
}
|
||||||
|
|
||||||
currentView = view;
|
currentView = view;
|
||||||
|
@ -60,10 +78,10 @@ public class Analytics {
|
||||||
|
|
||||||
|
|
||||||
public static synchronized void trackEvent(String category, String action, String label, Integer value) {
|
public static synchronized void trackEvent(String category, String action, String label, Integer value) {
|
||||||
if (!tracker.isEnabled())
|
if (!enabled)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
tracker.trackEvent(normalize(category), normalize(action), normalize(label), value);
|
getTracker().trackEvent(normalize(category), normalize(action), normalize(label), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -76,8 +94,8 @@ public class Analytics {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void setEnabled(boolean enabled) {
|
public static synchronized void setEnabled(boolean b) {
|
||||||
tracker.setEnabled(enabled);
|
enabled = b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -189,13 +207,20 @@ public class Analytics {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static VisitorData restoreVisitorData() {
|
private static final String VISITOR_ID = "visitorId";
|
||||||
|
private static final String TIMESTAMP_FIRST = "timestampFirst";
|
||||||
|
private static final String TIMESTAMP_LAST = "timestampLast";
|
||||||
|
private static final String VISITS = "visits";
|
||||||
|
|
||||||
|
|
||||||
|
private synchronized static VisitorData restoreVisitorData() {
|
||||||
try {
|
try {
|
||||||
// try to restore visitor
|
// try to restore visitor
|
||||||
int visitorId = Integer.parseInt(persistentData.get(VISITOR_ID));
|
Map<String, String> data = getPersistentData();
|
||||||
long timestampFirst = Long.parseLong(persistentData.get(TIMESTAMP_FIRST));
|
int visitorId = Integer.parseInt(data.get(VISITOR_ID));
|
||||||
long timestampLast = Long.parseLong(persistentData.get(TIMESTAMP_LAST));
|
long timestampFirst = Long.parseLong(data.get(TIMESTAMP_FIRST));
|
||||||
int visits = Integer.parseInt(persistentData.get(VISITS));
|
long timestampLast = Long.parseLong(data.get(TIMESTAMP_LAST));
|
||||||
|
int visits = Integer.parseInt(data.get(VISITS));
|
||||||
|
|
||||||
return VisitorData.newSession(visitorId, timestampFirst, timestampLast, visits);
|
return VisitorData.newSession(visitorId, timestampFirst, timestampLast, visits);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -205,28 +230,17 @@ public class Analytics {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static void storeVisitorData(VisitorData visitor) {
|
private synchronized static void storeVisitorData(VisitorData visitor) {
|
||||||
persistentData.put(VISITOR_ID, String.valueOf(visitor.getVisitorId()));
|
Map<String, String> data = getPersistentData();
|
||||||
persistentData.put(TIMESTAMP_FIRST, String.valueOf(visitor.getTimestampFirst()));
|
data.put(VISITOR_ID, String.valueOf(visitor.getVisitorId()));
|
||||||
persistentData.put(TIMESTAMP_LAST, String.valueOf(visitor.getTimestampPrevious()));
|
data.put(TIMESTAMP_FIRST, String.valueOf(visitor.getTimestampFirst()));
|
||||||
persistentData.put(VISITS, String.valueOf(visitor.getVisits()));
|
data.put(TIMESTAMP_LAST, String.valueOf(visitor.getTimestampPrevious()));
|
||||||
|
data.put(VISITS, String.valueOf(visitor.getVisits()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void completeTracking(long timeout) {
|
private static Map<String, String> getPersistentData() {
|
||||||
storeVisitorData(visitorData);
|
return Settings.forPackage(Analytics.class).node("analytics").asMap();
|
||||||
JGoogleAnalyticsTracker.completeBackgroundTasks(timeout);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static {
|
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread("AnalyticsShutdownHook") {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
completeTracking(2000);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -48,9 +48,6 @@ public class Main {
|
||||||
final ArgumentProcessor cli = new ArgumentProcessor();
|
final ArgumentProcessor cli = new ArgumentProcessor();
|
||||||
final ArgumentBean argumentBean = cli.parse(args);
|
final ArgumentBean argumentBean = cli.parse(args);
|
||||||
|
|
||||||
// initialize analytics
|
|
||||||
Analytics.setEnabled(!argumentBean.disableAnalytics);
|
|
||||||
|
|
||||||
if (argumentBean.printHelp()) {
|
if (argumentBean.printHelp()) {
|
||||||
// just print help message and exit afterwards
|
// just print help message and exit afterwards
|
||||||
cli.printHelp(argumentBean);
|
cli.printHelp(argumentBean);
|
||||||
|
@ -59,10 +56,14 @@ public class Main {
|
||||||
|
|
||||||
if (argumentBean.clearUserData()) {
|
if (argumentBean.clearUserData()) {
|
||||||
// clear preferences and cache
|
// clear preferences and cache
|
||||||
|
System.out.println("Reset preferences and clear cache.");
|
||||||
Settings.forPackage(Main.class).clear();
|
Settings.forPackage(Main.class).clear();
|
||||||
CacheManager.getInstance().clearAll();
|
CacheManager.getInstance().clearAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// initialize analytics
|
||||||
|
Analytics.setEnabled(!argumentBean.disableAnalytics);
|
||||||
|
|
||||||
// CLI mode => run command-line interface and then exit
|
// CLI mode => run command-line interface and then exit
|
||||||
if (argumentBean.runCLI()) {
|
if (argumentBean.runCLI()) {
|
||||||
int status = cli.process(argumentBean, new CmdlineOperations());
|
int status = cli.process(argumentBean, new CmdlineOperations());
|
||||||
|
|
|
@ -96,14 +96,7 @@ public class MainFrame extends JFrame {
|
||||||
|
|
||||||
|
|
||||||
protected PanelBuilder[] createPanelBuilders() {
|
protected PanelBuilder[] createPanelBuilders() {
|
||||||
return new PanelBuilder[] {
|
return new PanelBuilder[] { new ListPanelBuilder(), new RenamePanelBuilder(), new AnalyzePanelBuilder(), new EpisodeListPanelBuilder(), new SubtitlePanelBuilder(), new SfvPanelBuilder() };
|
||||||
new ListPanelBuilder(),
|
|
||||||
new RenamePanelBuilder(),
|
|
||||||
new AnalyzePanelBuilder(),
|
|
||||||
new EpisodeListPanelBuilder(),
|
|
||||||
new SubtitlePanelBuilder(),
|
|
||||||
new SfvPanelBuilder()
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -127,14 +120,18 @@ public class MainFrame extends JFrame {
|
||||||
|
|
||||||
if (panel == null) {
|
if (panel == null) {
|
||||||
panel = selectedBuilder.create();
|
panel = selectedBuilder.create();
|
||||||
|
panel.setVisible(false); // invisible by default
|
||||||
panel.putClientProperty("panelBuilder", selectedBuilder);
|
panel.putClientProperty("panelBuilder", selectedBuilder);
|
||||||
|
|
||||||
contentPane.add(panel);
|
contentPane.add(panel);
|
||||||
}
|
}
|
||||||
|
|
||||||
Analytics.trackView(panel.getClass(), selectedBuilder.getName());
|
// make visible, ignore action is visible already
|
||||||
headerPanel.setTitle(selectedBuilder.getName());
|
if (!panel.isVisible()) {
|
||||||
panel.setVisible(true);
|
headerPanel.setTitle(selectedBuilder.getName());
|
||||||
|
panel.setVisible(true);
|
||||||
|
Analytics.trackView(panel.getClass(), selectedBuilder.getName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue