Public Member Functions | Protected Member Functions | List of all members
ThreadedAnalyticsDestination Class Referenceabstract

A base class for dispatching analytics events on a dedicated thread. More...

Inheritance diagram for ThreadedAnalyticsDestination:

Public Member Functions

 ThreadedAnalyticsDestination (const String &threadName="Analytics thread")
 Creates a ThreadedAnalyticsDestination. More...
 
virtual ~ThreadedAnalyticsDestination ()
 Destructor. More...
 
virtual int getMaximumBatchSize ()=0
 Override this method to provide the maximum batch size you can handle in your subclass. More...
 
virtual bool logBatchedEvents (const Array< AnalyticsEvent > &events)=0
 This method will be called periodically on the analytics thread. More...
 
virtual void stopLoggingEvents ()=0
 You must always call stopAnalyticsThread in the destructor of your subclass (or before then) to give the analytics thread time to shut down. More...
 
void setBatchPeriod (int newSubmissionPeriodMilliseconds)
 Call this to set the period between logBatchedEvents invocations. More...
 
void logEvent (const AnalyticsEvent &event) override final
 Adds an event to the queue, which will ultimately be submitted to logBatchedEvents. More...
 
- Public Member Functions inherited from AnalyticsDestination
 AnalyticsDestination ()=default
 Constructor. More...
 
virtual ~AnalyticsDestination ()
 Destructor. More...
 

Protected Member Functions

void startAnalyticsThread (int initialBatchPeriodMilliseconds)
 Starts the analytics thread, with an initial event batching period. More...
 
void stopAnalyticsThread (int timeoutMilliseconds)
 Triggers the shutdown of the analytics thread. More...
 

Detailed Description

A base class for dispatching analytics events on a dedicated thread.

This class is particularly useful for sending analytics events to a web server without blocking the message thread. It can also save (and restore) events that were not dispatched so no information is lost when an internet connection is absent or something else prevents successful logging.

Once startAnalyticsThread is called the logBatchedEvents method is periodically invoked on an analytics thread, with the period determined by calls to setBatchPeriod. Here events are grouped together into batches, with the maximum batch size set by the implementation of getMaximumBatchSize.

It's important to call stopAnalyticsThread in the destructor of your subclass (or before then) to give the analytics thread time to shut down. Calling stopAnalyticsThread will, in turn, call stopLoggingEvents, which you should use to terminate the currently running logBatchedEvents call.

See also
Analytics, AnalyticsDestination, AnalyticsDestination::AnalyticsEvent

Constructor & Destructor Documentation

◆ ThreadedAnalyticsDestination()

ThreadedAnalyticsDestination::ThreadedAnalyticsDestination ( const String threadName = "Analytics thread")

Creates a ThreadedAnalyticsDestination.

Parameters
threadNameused to identify the analytics thread in debug builds

◆ ~ThreadedAnalyticsDestination()

virtual ThreadedAnalyticsDestination::~ThreadedAnalyticsDestination ( )
virtual

Destructor.

Member Function Documentation

◆ getMaximumBatchSize()

virtual int ThreadedAnalyticsDestination::getMaximumBatchSize ( )
pure virtual

Override this method to provide the maximum batch size you can handle in your subclass.

Calls to logBatchedEvents will contain no more than this number of events.

◆ logBatchedEvents()

virtual bool ThreadedAnalyticsDestination::logBatchedEvents ( const Array< AnalyticsEvent > &  events)
pure virtual

This method will be called periodically on the analytics thread.

If this method returns false then the subsequent call of this function will contain the same events as previous call, plus any new events that have been generated in the period between calls. The order of events will not be changed. This allows you to retry logging events until they are logged successfully.

Parameters
eventsa list of events to be logged
Returns
if the events were successfully logged

◆ stopLoggingEvents()

virtual void ThreadedAnalyticsDestination::stopLoggingEvents ( )
pure virtual

You must always call stopAnalyticsThread in the destructor of your subclass (or before then) to give the analytics thread time to shut down.

Calling stopAnalyticsThread triggers a call to this method. At this point you are guaranteed that logBatchedEvents has been called for the last time and you should make sure that the current call to logBatchedEvents finishes as quickly as possible. This method and a subsequent call to saveUnloggedEvents must both complete before the timeout supplied to stopAnalyticsThread.

In a normal use case stopLoggingEvents will be called on the message thread from the destructor of your ThreadedAnalyticsDestination subclass, and must stop the logBatchedEvents method which is running on the analytics thread.

See also
stopAnalyticsThread

◆ setBatchPeriod()

void ThreadedAnalyticsDestination::setBatchPeriod ( int  newSubmissionPeriodMilliseconds)

Call this to set the period between logBatchedEvents invocations.

This method is thread safe and can be used to implements things like exponential backoff in logBatchedEvents calls.

Parameters
newSubmissionPeriodMillisecondsthe new submission period to use in milliseconds

◆ logEvent()

void ThreadedAnalyticsDestination::logEvent ( const AnalyticsEvent event)
finaloverridevirtual

Adds an event to the queue, which will ultimately be submitted to logBatchedEvents.

This method is thread safe.

Parameters
eventthe analytics event to add to the queue

Implements AnalyticsDestination.

◆ startAnalyticsThread()

void ThreadedAnalyticsDestination::startAnalyticsThread ( int  initialBatchPeriodMilliseconds)
protected

Starts the analytics thread, with an initial event batching period.

Parameters
initialBatchPeriodMillisecondsthe initial event batching period in milliseconds

◆ stopAnalyticsThread()

void ThreadedAnalyticsDestination::stopAnalyticsThread ( int  timeoutMilliseconds)
protected

Triggers the shutdown of the analytics thread.

You must call this method in the destructor of your subclass (or before then) to give the analytics thread time to shut down.

This method invokes stopLoggingEvents and you should ensure that both the analytics thread and a call to saveUnloggedEvents are able to finish before the supplied timeout. This timeout is important because on platforms like iOS an app is killed if it takes too long to shut down.

Parameters
timeoutMillisecondsthe number of milliseconds before the analytics thread is forcibly terminated

The documentation for this class was generated from the following file: