File: GoogleAnalyticsTracker.java

package info (click to toggle)
libjaba-client-java 2.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 2,052 kB
  • sloc: java: 17,308; makefile: 12
file content (305 lines) | stat: -rw-r--r-- 9,118 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package compbio.stat.ga;

import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.SocketAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.regex.MatchResult;

import org.apache.log4j.Logger;

public class GoogleAnalyticsTracker {

	private static Logger logger = Logger.getLogger(GoogleAnalyticsTracker.class);
	static Proxy proxy = Proxy.NO_PROXY;

	final AnalyticsConfigData configData;
	final GoogleAnalyticsV4_7_2 builder;

	private static ExecutorService EXECUTOR = null;

	private ExecutorService getExecutor() {
		if (EXECUTOR == null) {
			synchronized (GoogleAnalyticsTracker.class) {
				if (EXECUTOR == null) {
					EXECUTOR = Executors.newFixedThreadPool(2,
						    new ThreadFactory() {
						        public Thread newThread(Runnable runnable) {
						            Thread thread = Executors.defaultThreadFactory().newThread(runnable);
						            thread.setDaemon(true);
						            return thread;
						        }
						    });
					//EXECUTOR = Executors.newFixedThreadPool(2);
					Runtime.getRuntime().addShutdownHook(new Thread() {

						@Override
						public void run() {
							try {
								EXECUTOR.shutdown();
								EXECUTOR.awaitTermination(1, TimeUnit.SECONDS);
							} catch (InterruptedException ignored) {
								EXECUTOR.shutdownNow();
							}
						}
					});
				}
			}
		}
		return EXECUTOR;
	}

	public GoogleAnalyticsTracker(AnalyticsConfigData argConfigData) {
		configData = argConfigData;
		builder = new GoogleAnalyticsV4_7_2(configData);
	}

	/**
	 * Define the proxy to use for all GA tracking requests.
	 * <p>
	 * Call this static method early (before creating any tracking requests).
	 * 
	 * @param argProxy
	 *            The proxy to use
	 */
	public static void setProxy(Proxy argProxy) {
		proxy = (argProxy != null) ? argProxy : Proxy.NO_PROXY;
	}

	/**
	 * Define the proxy to use for all GA tracking requests.
	 * <p>
	 * Call this static method early (before creating any tracking requests).
	 * 
	 * @param proxyAddr
	 *            "addr:port" of the proxy to use; may also be given as URL
	 *            ("http://addr:port/").
	 */
	public static void setProxy(String proxyAddr) {
		if (proxyAddr != null) {
			Scanner s = new Scanner(proxyAddr);

			// Split into "proxyAddr:proxyPort".
			proxyAddr = null;
			int proxyPort = 8080;
			try {
				s.findInLine("(http://|)([^:/]+)(:|)([0-9]*)(/|)");
				MatchResult m = s.match();

				if (m.groupCount() >= 2) {
					proxyAddr = m.group(2);
				}

				if ((m.groupCount() >= 4) && (!m.group(4).isEmpty())) {
					proxyPort = Integer.parseInt(m.group(4));
				}
			} finally {
				s.close();
			}

			if (proxyAddr != null) {
				SocketAddress sa = new InetSocketAddress(proxyAddr, proxyPort);
				setProxy(new Proxy(Type.HTTP, sa));
			}
		}
	}

	/**
	 * Tracks a page view.
	 * 
	 * @param argPageURL
	 *            required, Google won't track without it. Ex:
	 *            <code>"org/me/javaclass.java"</code>, or anything you want as
	 *            the page url.
	 * @param argPageTitle
	 *            content title
	 * @param argHostName
	 *            the host name for the url
	 * @param argReferrerSite
	 *            site of the referrer. ex, www.dmurph.com
	 * @param argReferrerPage
	 *            page of the referrer. ex, /mypage.php
	 */
	public void trackPageViewFromReferrer(String argPageURL,
			String argPageTitle, String argHostName, String argReferrerSite,
			String argReferrerPage) {
		if (argPageURL == null) {
			throw new IllegalArgumentException(
					"Page URL cannot be null, Google will not track the data.");
		}
		AnalyticsRequestData data = new AnalyticsRequestData();
		data.setHostName(argHostName);
		data.setPageTitle(argPageTitle);
		data.setPageURL(argPageURL);
		data.setReferrer(argReferrerSite, argReferrerPage);
		makeCustomRequest(data);
	}

	/**
	 * Tracks a page view.
	 * 
	 * @param argPageURL
	 *            required, Google won't track without it. Ex:
	 *            <code>"org/me/javaclass.java"</code>, or anything you want as
	 *            the page url.
	 * @param argPageTitle
	 *            content title
	 * @param argHostName
	 *            the host name for the url
	 * @param argReferrerSite
	 *            site of the referrer. ex, www.dmurph.com
	 * @param argReferrerPage
	 *            page of the referrer. ex, /mypage.php
	 */
	public void trackPageViewWithLocalhostAsReferrer(String argPageURL,
			String argPageTitle, String argHostName, String argReferrerPage) {
		if (argPageURL == null) {
			throw new IllegalArgumentException(
					"Page URL cannot be null, Google will not track the data.");
		}
		AnalyticsRequestData data = new AnalyticsRequestData();
		data.setHostName(argHostName);
		data.setPageTitle(argPageTitle);
		data.setPageURL(argPageURL);
		String localAddr = "127.0.0.1";
		try {
			InetAddress localAddrIP = InetAddress.getLocalHost();
			localAddr = localAddrIP.getCanonicalHostName();
		} catch (UnknownHostException ignored) {
		}
		data.setReferrer(localAddr, argReferrerPage);
		makeCustomRequest(data);
	}

	/**
	 * Tracks a page view.
	 * 
	 * @param argPageURL
	 *            required, Google won't track without it. Ex:
	 *            <code>"org/me/javaclass.java"</code>, or anything you want as
	 *            the page url.
	 * @param argPageTitle
	 *            content title
	 * @param argHostName
	 *            the host name for the url
	 * @param argSearchSource
	 *            source of the search engine. ex: google
	 * @param argSearchKeywords
	 *            the keywords of the search. ex: java google analytics tracking
	 *            utility
	 */
	public void trackPageViewFromSearch(String argPageURL, String argPageTitle,
			String argHostName, String argSearchSource, String argSearchKeywords) {
		if (argPageURL == null) {
			throw new IllegalArgumentException(
					"Page URL cannot be null, Google will not track the data.");
		}
		AnalyticsRequestData data = new AnalyticsRequestData();
		data.setHostName(argHostName);
		data.setPageTitle(argPageTitle);
		data.setPageURL(argPageURL);
		data.setSearchReferrer(argSearchSource, argSearchKeywords);
		makeCustomRequest(data);
	}

	/**
	 * 
	 * 
	 * @param data
	 */
	private void makeCustomRequest(AnalyticsRequestData data) {
		final String url = builder.buildURL(data);
		getExecutor().execute(new Runnable() {

			@Override
			public void run() {
				dispatchRequest(url);
			}
		});
	}

	private final static void dispatchRequest(String argURL) {
		try {
			URL url = new URL(argURL);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection(proxy);
			connection.setRequestMethod("GET");
			connection.setInstanceFollowRedirects(true);
			connection.connect();
			int responseCode = connection.getResponseCode();
			if (responseCode != HttpURLConnection.HTTP_OK) {
				logger.error("JGoogleAnalyticsTracker: Error requesting url '{}', received response code {} "
						+ argURL + responseCode);
			} else {
				logger.debug("JGoogleAnalyticsTracker: Tracking success for url '{} '"
						+ argURL);
			}
		} catch (Exception e) {
			logger.error("Error making tracking request", e);
		}
	}

	/**
	 * Tracks an event. To provide more info about the page, use
	 * {@link #makeCustomRequest(AnalyticsRequestData)}.
	 * 
	 * @param argCategory
	 * @param argAction
	 */
	public void trackEvent(String argCategory, String argAction) {
		trackEvent(argCategory, argAction, null, null);
	}

	/**
	 * Tracks an event. To provide more info about the page, use
	 * {@link #makeCustomRequest(AnalyticsRequestData)}.
	 * 
	 * @param argCategory
	 * @param argAction
	 * @param argLabel
	 */
	public void trackEvent(String argCategory, String argAction, String argLabel) {
		trackEvent(argCategory, argAction, argLabel, null);
	}

	/**
	 * Tracks an event. To provide more info about the page, use
	 * {@link #makeCustomRequest(AnalyticsRequestData)}.
	 * 
	 * @param argCategory
	 *            required
	 * @param argAction
	 *            required
	 * @param argLabel
	 *            optional
	 * @param argValue
	 *            optional
	 */
	public void trackEvent(String argCategory, String argAction,
			String argLabel, Integer argValue) {
		AnalyticsRequestData data = new AnalyticsRequestData();
		data.setEventCategory(argCategory);
		data.setEventAction(argAction);
		data.setEventLabel(argLabel);
		data.setEventValue(argValue);

		makeCustomRequest(data);
	}

	/**
	 * Resets the session cookie.
	 */
	public void resetSession() {
		builder.resetSession();
	}

}