class MixpanelPlugin(Plugin): def __init__(self, api_key: str, options: Optional[MixpanelOptions] = None) -> None: self._api_key: str = api_key self._options: MixpanelOptions = options if options is not None else MixpanelOptions() self._client: Optional[Mixpanel] = None self._consumer: Optional[BufferedConsumer] = None self._logger: Logger = Logger.NONE def id(self) -> str: return 'mixpanel' def load(self, options: PluginLoadOptions) -> None: self._consumer = BufferedConsumer( max_size=self._options.flush_queue_size, request_timeout=int(self._options.request_timeout.total_seconds()), events_url=None if self._options.api_host is None else f'{self._options.api_host}/track', people_url=None if self._options.api_host is None else f'{self._options.api_host}/engage', ) self._client = Mixpanel( token=self._api_key, consumer=self._consumer, ) self._logger = options.logger def alias(self, user_id: str, previous_id: str) -> None: assert self._client is not None self._client.alias( original=user_id, alias_id=previous_id) def identify(self, user_id: str, properties: Optional[Properties]) -> None: assert self._client is not None self._client.people_update({ '$distinct_id': user_id, '$set': properties.to_json() if properties is not None else {}, }) def track(self, user_id: str, event: Event) -> None: assert self._client is not None self._client.track( distinct_id=user_id, event_name=event.name, properties=event.properties.to_json() if event.properties is not None else None) def flush(self) -> None: assert self._consumer is not None self._consumer.flush() def shutdown(self) -> None: assert self._consumer is not None self._consumer.flush() def _on_error(self, err: str) -> None: self._logger.error(f"Error. {err}")
def load(self, options: PluginLoadOptions) -> None: self._consumer = BufferedConsumer( max_size=self._options.flush_queue_size, request_timeout=int(self._options.request_timeout.total_seconds()), events_url=None if self._options.api_host is None else f'{self._options.api_host}/track', people_url=None if self._options.api_host is None else f'{self._options.api_host}/engage', ) self._client = Mixpanel( token=self._api_key, consumer=self._consumer, ) self._logger = options.logger
def do_sending(queue): """ This process is the analytics worker process- it can wait on HTTP responses to Mixpanel without blocking other jobs. This might be a queue consumer process or just a separate thread from the code that observes the things you want to measure. """ consumer = BufferedConsumer() payload = queue.get() while payload is not None: consumer.send(*payload) payload = queue.get() consumer.flush()
class SendToMixpanel(Thread): def __init__(self, request_queue): Thread.__init__(self) self.daemon = True self._mp_queue = request_queue self._consumer = BufferedConsumer() def run(self): logger.debug("Starting mixpanel sender process.") while True: mp_request = self._mp_queue.get() logger.debug("Got queued mixpanel request.") try: self._consumer.send(*json.loads(mp_request)) except: logger.exception("Failed to send Mixpanel request.")
def do_sending(queue): ''' This process is the analytics worker process- it can wait on HTTP responses to Mixpanel without blocking other jobs. This might be a queue consumer process or just a separate thread from the code that observes the things you want to measure. ''' consumer = BufferedConsumer() payload = queue.get() while payload is not None: consumer.send(*payload) payload = queue.get() consumer.flush()
def __init__(self, request_queue): Thread.__init__(self) self.daemon = True self._mp_queue = request_queue self._consumer = BufferedConsumer()
from __future__ import unicode_literals from django.conf import settings from django.db.models.signals import post_save from django.dispatch import receiver from django_rq import job from mixpanel import Mixpanel, BufferedConsumer from shoutit.models.auth import AbstractProfile from shoutit.monkey_patches import ShoutitJSONEncoder from ..models import User from ..utils import debug_logger # Shoutit mixpanel MAX_MP_BUFFER_SIZE = 50 buffered_consumer = BufferedConsumer(max_size=MAX_MP_BUFFER_SIZE) shoutit_mp = Mixpanel(settings.MIXPANEL_TOKEN, serializer=ShoutitJSONEncoder) shoutit_mp_buffered = Mixpanel(settings.MIXPANEL_TOKEN, consumer=buffered_consumer, serializer=ShoutitJSONEncoder) def alias(alias_id, original, event_name=None, track_properties=None, add=False): if not settings.USE_MIXPANEL: return return _alias.delay(alias_id, original,