This Demo App shows how you can override the default Optimizely Event Dispatcher
, and instead enqueue events within Redis and dispatch them at a later time as a large batch request, rather than many single event tracking requests. This helps minimize the number of outbound network requests from your application server.
This Demo App is based on this repository. Follow the installation and setup instructions prior to advancing to the Redis configuration outlined below.
In order for the Optimizely SDK to instiate properly, activate the correct experiment, and fire the correct events, you'll have to either
- Set the
entity_keys
as ENV variables- demoapp_PROJECT_ID
- demoapp_EXPERIMENT_KEY
- demoapp_EVENT_KEY
- Hardcode the
entity_keys
within theoptimizely_entity_conf.py
file
This app uses Redis-Py to connect to Redis. This dependency lives within the requirements.txt
file, and will be installed automatically when following installation instructions.
The Redis connection settings live within the redisbroker.py file. Adapt the connection settings to reflect the location of your running redis instance.
When instantiating the Optimizely client, pass the argument event_dispatcher=RedisDeferredDispatcher()
as shown below:
from optimizely import optimizely
from redisdeferreddispatcher import RedisDeferredDispatcher
client = optimizely.Optimizely(datafile, event_dispatcher=RedisDeferredDispatcher())
It's important an instance is passed into the constructor, not the class itself
At some scheduled interval, run the command that will dispatch the queued events to Optimizely and purge them from the local Redis store.
$ python redisbroker.py --emit