Class JGoogleAnalyticsTracker
- java.lang.Object
-
- com.dmurph.tracking.JGoogleAnalyticsTracker
-
public class JGoogleAnalyticsTracker extends java.lang.Object
Common tracking calls are implemented as methods, but if you want to control what data to send, then usemakeCustomRequest(AnalyticsRequestData)
. If you are making custom calls, the only requirements are:- If you are tracking an event,
AnalyticsRequestData.setEventCategory(String)
andAnalyticsRequestData.setEventAction(String)
must both be populated. - If you are not tracking an event,
AnalyticsRequestData.setPageURL(String)
must be populated
The tracker can operate in three modes:
- synchronous mode: The HTTP request is sent to GA immediately, before the track method returns. This may slow your application down if GA doesn't respond fast.
- multi-thread mode: Each track method call creates a new short-lived thread that sends the HTTP request to GA in the background and terminates.
- single-thread mode (the default): The track method stores the request in a FIFO and returns immediately. A single long-lived background thread consumes the FIFO content and sends the HTTP requests to GA.
To halt the background thread safely, use the call
stopBackgroundThread(long)
, where the parameter is the timeout to wait for any remaining queued tracking calls to be made. Keep in mind that if new tracking requests are made after the thread is stopped, they will just be stored in the queue, and will not be sent to GA until the thread is started again withstartBackgroundThread()
(This is assuming you are in single-threaded mode to begin with). - If you are tracking an event,
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
JGoogleAnalyticsTracker.DispatchMode
static class
JGoogleAnalyticsTracker.GoogleAnalyticsVersion
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.ThreadGroup
asyncThreadGroup
private static long
asyncThreadsRunning
private static java.lang.Thread
backgroundThread
private static boolean
backgroundThreadMayRun
private IGoogleAnalyticsURLBuilder
builder
private AnalyticsConfigData
configData
private boolean
enabled
private static java.util.LinkedList<java.lang.String>
fifo
private JGoogleAnalyticsTracker.GoogleAnalyticsVersion
gaVersion
private static org.slf4j.Logger
logger
private JGoogleAnalyticsTracker.DispatchMode
mode
private static java.net.Proxy
proxy
-
Constructor Summary
Constructors Constructor Description JGoogleAnalyticsTracker(AnalyticsConfigData argConfigData, JGoogleAnalyticsTracker.GoogleAnalyticsVersion argVersion)
JGoogleAnalyticsTracker(AnalyticsConfigData argConfigData, JGoogleAnalyticsTracker.GoogleAnalyticsVersion argVersion, JGoogleAnalyticsTracker.DispatchMode argMode)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
completeBackgroundTasks(long timeoutMillis)
Wait for background tasks to complete.private void
createBuilder()
private static void
dispatchRequest(java.lang.String argURL)
JGoogleAnalyticsTracker.DispatchMode
getDispatchMode()
Gets the current dispatch mode.boolean
isEnabled()
If the api is dispatching tracking requests (default of true).boolean
isMultiThreaded()
Convenience method to check if the tracker is in multi-thread modeboolean
isSingleThreaded()
Convenience method to check if the tracker is in single-thread modeboolean
isSynchronous()
Convenience method to check if the tracker is in synchronous mode.void
makeCustomRequest(AnalyticsRequestData argData)
Makes a custom tracking request based from the given data.void
resetSession()
Resets the session cookie.void
setDispatchMode(JGoogleAnalyticsTracker.DispatchMode argMode)
Sets the dispatch modevoid
setEnabled(boolean argEnabled)
Sets if the api dispatches tracking requests.static void
setProxy(java.lang.String proxyAddr)
Define the proxy to use for all GA tracking requests.static void
setProxy(java.net.Proxy argProxy)
Define the proxy to use for all GA tracking requests.private static void
startBackgroundThread()
If the background thread for 'queued' mode is not running, start it now.static void
stopBackgroundThread(long timeoutMillis)
Stop the long-lived background thread.void
trackEvent(java.lang.String argCategory, java.lang.String argAction)
Tracks an event.void
trackEvent(java.lang.String argCategory, java.lang.String argAction, java.lang.String argLabel)
Tracks an event.void
trackEvent(java.lang.String argCategory, java.lang.String argAction, java.lang.String argLabel, java.lang.Integer argValue)
Tracks an event.void
trackPageView(java.lang.String argPageURL, java.lang.String argPageTitle, java.lang.String argHostName)
Tracks a page view.void
trackPageViewFromReferrer(java.lang.String argPageURL, java.lang.String argPageTitle, java.lang.String argHostName, java.lang.String argReferrerSite, java.lang.String argReferrerPage)
Tracks a page view.void
trackPageViewFromSearch(java.lang.String argPageURL, java.lang.String argPageTitle, java.lang.String argHostName, java.lang.String argSearchSource, java.lang.String argSearchKeywords)
Tracks a page view.
-
-
-
Field Detail
-
logger
private static org.slf4j.Logger logger
-
asyncThreadGroup
private static final java.lang.ThreadGroup asyncThreadGroup
-
asyncThreadsRunning
private static long asyncThreadsRunning
-
proxy
private static java.net.Proxy proxy
-
fifo
private static java.util.LinkedList<java.lang.String> fifo
-
backgroundThread
private static java.lang.Thread backgroundThread
-
backgroundThreadMayRun
private static boolean backgroundThreadMayRun
-
gaVersion
private JGoogleAnalyticsTracker.GoogleAnalyticsVersion gaVersion
-
configData
private AnalyticsConfigData configData
-
builder
private IGoogleAnalyticsURLBuilder builder
-
mode
private JGoogleAnalyticsTracker.DispatchMode mode
-
enabled
private boolean enabled
-
-
Constructor Detail
-
JGoogleAnalyticsTracker
public JGoogleAnalyticsTracker(AnalyticsConfigData argConfigData, JGoogleAnalyticsTracker.GoogleAnalyticsVersion argVersion)
-
JGoogleAnalyticsTracker
public JGoogleAnalyticsTracker(AnalyticsConfigData argConfigData, JGoogleAnalyticsTracker.GoogleAnalyticsVersion argVersion, JGoogleAnalyticsTracker.DispatchMode argMode)
-
-
Method Detail
-
setDispatchMode
public void setDispatchMode(JGoogleAnalyticsTracker.DispatchMode argMode)
Sets the dispatch mode- Parameters:
argMode
- the mode to to put the tracker in. If this is null, the tracker defaults toJGoogleAnalyticsTracker.DispatchMode.SINGLE_THREAD
- See Also:
JGoogleAnalyticsTracker.DispatchMode
-
getDispatchMode
public JGoogleAnalyticsTracker.DispatchMode getDispatchMode()
Gets the current dispatch mode. Default isJGoogleAnalyticsTracker.DispatchMode.SINGLE_THREAD
.- Returns:
- See Also:
JGoogleAnalyticsTracker.DispatchMode
-
isSynchronous
public boolean isSynchronous()
Convenience method to check if the tracker is in synchronous mode.- Returns:
-
isSingleThreaded
public boolean isSingleThreaded()
Convenience method to check if the tracker is in single-thread mode- Returns:
-
isMultiThreaded
public boolean isMultiThreaded()
Convenience method to check if the tracker is in multi-thread mode- Returns:
-
resetSession
public void resetSession()
Resets the session cookie.
-
setEnabled
public void setEnabled(boolean argEnabled)
Sets if the api dispatches tracking requests.- Parameters:
argEnabled
-
-
isEnabled
public boolean isEnabled()
If the api is dispatching tracking requests (default of true).- Returns:
-
setProxy
public static void setProxy(java.net.Proxy argProxy)
Define the proxy to use for all GA tracking requests.Call this static method early (before creating any tracking requests).
- Parameters:
argProxy
- The proxy to use
-
setProxy
public static void setProxy(java.lang.String proxyAddr)
Define the proxy to use for all GA tracking requests.Call this static method early (before creating any tracking requests).
- Parameters:
proxyAddr
- "addr:port" of the proxy to use; may also be given as URL ("http://addr:port/").
-
completeBackgroundTasks
public static void completeBackgroundTasks(long timeoutMillis)
Wait for background tasks to complete.This works in queued and asynchronous mode.
- Parameters:
timeoutMillis
- The maximum number of milliseconds to wait.
-
trackPageView
public void trackPageView(java.lang.String argPageURL, java.lang.String argPageTitle, java.lang.String argHostName)
Tracks a page view.- Parameters:
argPageURL
- required, Google won't track without it. Ex:"org/me/javaclass.java"
, or anything you want as the page url.argPageTitle
- content titleargHostName
- the host name for the url
-
trackPageViewFromReferrer
public void trackPageViewFromReferrer(java.lang.String argPageURL, java.lang.String argPageTitle, java.lang.String argHostName, java.lang.String argReferrerSite, java.lang.String argReferrerPage)
Tracks a page view.- Parameters:
argPageURL
- required, Google won't track without it. Ex:"org/me/javaclass.java"
, or anything you want as the page url.argPageTitle
- content titleargHostName
- the host name for the urlargReferrerSite
- site of the referrer. ex, www.dmurph.comargReferrerPage
- page of the referrer. ex, /mypage.php
-
trackPageViewFromSearch
public void trackPageViewFromSearch(java.lang.String argPageURL, java.lang.String argPageTitle, java.lang.String argHostName, java.lang.String argSearchSource, java.lang.String argSearchKeywords)
Tracks a page view.- Parameters:
argPageURL
- required, Google won't track without it. Ex:"org/me/javaclass.java"
, or anything you want as the page url.argPageTitle
- content titleargHostName
- the host name for the urlargSearchSource
- source of the search engine. ex: googleargSearchKeywords
- the keywords of the search. ex: java google analytics tracking utility
-
trackEvent
public void trackEvent(java.lang.String argCategory, java.lang.String argAction)
Tracks an event. To provide more info about the page, usemakeCustomRequest(AnalyticsRequestData)
.- Parameters:
argCategory
-argAction
-
-
trackEvent
public void trackEvent(java.lang.String argCategory, java.lang.String argAction, java.lang.String argLabel)
Tracks an event. To provide more info about the page, usemakeCustomRequest(AnalyticsRequestData)
.- Parameters:
argCategory
-argAction
-argLabel
-
-
trackEvent
public void trackEvent(java.lang.String argCategory, java.lang.String argAction, java.lang.String argLabel, java.lang.Integer argValue)
Tracks an event. To provide more info about the page, usemakeCustomRequest(AnalyticsRequestData)
.- Parameters:
argCategory
- requiredargAction
- requiredargLabel
- optionalargValue
- optional
-
makeCustomRequest
public void makeCustomRequest(AnalyticsRequestData argData)
Makes a custom tracking request based from the given data.- Parameters:
argData
-- Throws:
java.lang.NullPointerException
- if argData is null or if the URL builder is null
-
dispatchRequest
private static void dispatchRequest(java.lang.String argURL)
-
createBuilder
private void createBuilder()
-
startBackgroundThread
private static void startBackgroundThread()
If the background thread for 'queued' mode is not running, start it now.
-
stopBackgroundThread
public static void stopBackgroundThread(long timeoutMillis)
Stop the long-lived background thread.This method is needed for debugging purposes only. Calling it in an application is not really required: The background thread will terminate automatically when the application exits.
- Parameters:
timeoutMillis
- If nonzero, wait for thread completion before returning.
-
-