def run_query(self, query, user): import mixpanel api_secret = self.configuration.get('api_secret') api = mixpanel.Mixpanel(api_secret) data = api.request(["jql"], {"script": query}, "POST") result = {} if (not isinstance(data[0], Iterable)) or type( data[0]) in [str, unicode]: result["columns"] = [{"name": "value", "friendly_name": "value"}] result["rows"] = [{"value": v} for v in data] elif isinstance(data[0], dict): result["columns"] = [{ "name": k, "friendly_name": k } for k in data[0]] result["rows"] = data else: result["columns"] = [{ "name": k, "friendly_name": k } for k in range(len(data[0]))] result["rows"] = data print result["columns"] return json.dumps(result), None
def analytics_user_update( payload, proxy=None ): """ Update a user's info on the analytics service """ global ANALYTICS_KEY try: import mixpanel except: log.debug("mixpanel is not installed; no analytics will be reported") return False conf = get_config(config_path) if conf is None: log.debug("Failed to load config") return False if not conf['anonymous_statistics']: return False u = conf['uuid'] if ANALYTICS_KEY is None: ANALYTICS_KEY = get_analytics_key( u ) if ANALYTICS_KEY is None: return False # update the user log.debug("Update user '%s'" % u) mp = mixpanel.Mixpanel(ANALYTICS_KEY) mp.people_append( u, payload ) return True
def __init__(self, urls, **kwargs): cookie_secret = kwargs.pop('cookie_secret', None) if cookie_secret is None: cookie_secret = config['general'].get('cookie_secret', None) super(Application, self).__init__(urls, debug=config['general'].getboolean('debug'), cookie_secret=cookie_secret, **kwargs) if 'executor' in config: max_workers = config['executor'].getint('max_workers', None) else: max_workers = None self.executor = concurrent.futures.ThreadPoolExecutor( max_workers=max_workers) if 'mixpanel' in config and 'token' in config['mixpanel']: try: from dgas.analytics import TornadoMixpanelConsumer import mixpanel self.mixpanel_consumer = TornadoMixpanelConsumer() self.mixpanel_instance = mixpanel.Mixpanel( config['mixpanel']['token'], consumer=self.mixpanel_consumer) except: log.warning( "Mixpanel is configured, but the mixpanel-python library hasn't been installed" ) self.mixpanel_instance = None else: self.mixpanel_instance = None
def analytics_user_register(u, email, config_path=CONFIG_PATH, proxy=None): """ Register a user with the analytics service """ global ANALYTICS_KEY try: import mixpanel except: log.debug('mixpanel is not installed; no analytics will be reported') return False conf = get_config(path=config_path) if conf is None: log.debug('Failed to load config') return False if not conf['anonymous_statistics']: return False ANALYTICS_KEY = get_analytics_key(u) if ANALYTICS_KEY is None else ANALYTICS_KEY if ANALYTICS_KEY is None: return False # register the user log.debug('Register user "{}"'.format(u)) mp = mixpanel.Mixpanel(ANALYTICS_KEY) mp.people_set_once(u, {}) return True
def analytics_user_update(payload, proxy=None, config_path=CONFIG_PATH): """ Update a user's info on the analytics service """ global ANALYTICS_KEY try: import mixpanel except: log.debug('mixpanel is not installed; no analytics will be reported') return False conf = get_config(config_path) if conf is None: log.debug('Failed to load config') return False if not conf['anonymous_statistics']: return False u = conf['uuid'] ANALYTICS_KEY = get_analytics_key(u) if ANALYTICS_KEY is None else ANALYTICS_KEY if ANALYTICS_KEY is None: return False # update the user log.debug('Update user "{}"'.format(u)) mp = mixpanel.Mixpanel(ANALYTICS_KEY) mp.people_append(u, payload) return True
def test_connection(self): import mixpanel sample_query = "main = () => People().reduce(mixpanel.reducer.null())" api_secret = self.configuration.get('api_secret') api = mixpanel.Mixpanel(api_secret) data = api.request(["jql"], {"script": sample_query}, "POST") if data != [None]: raise Exception("Can't fetch from JQL")
class _MetricsWorkerThread(threading.Thread): """Worker Thread for publishing metrics in the background.""" def __init__(self, mode, source): threading.Thread.__init__(self, name='metrics-worker') if CONFIG.version.endswith('.gpu'): self._version = CONFIG.version.split('.gpu')[0] self._isgpu = True else: self._version = CONFIG.version self._isgpu = False self._mode = mode self._source = source try: # product key from .. import product_key self._product_key = product_key.get_product_key() except Exception, e: self._product_key = None self.queue = METRICS_QUEUE root_package_name = __import__(__name__.split('.')[0]).__name__ self.logger = logging.getLogger(root_package_name + '.metrics') self._tracker = None # librato metrics tracker self._mixpanel = None # Mixpanel metrics tracker buffer_size = 5 offline_buffer_size = 25 self._sys_info_set = False self._usable = False try: self._metrics_url = CONFIG.metrics_url self._requests = _requests # support mocking out requests library in unit-tests if self._mode != 'PROD': self.logger.info( "Using MetricMock instead of real metrics, mode is: %s" % self._mode) self._tracker = MetricMock() self._mixpanel = MetricMock() else: self._tracker = librato.connect(CONFIG.librato_user, CONFIG.librato_token) self._mixpanel = mixpanel.Mixpanel(CONFIG.mixpanel_user) except Exception, e: self.logger.warning( "Unexpected exception connecting to Metrics service, disabling metrics, exception %s" % e)
def analytics_event(event_type, event_payload, config_path=CONFIG_PATH, proxy=None, analytics_key=None, action_tag='Perform action'): """ Log an analytics event Return True if logged Return False if not The client uses 'Perform action' as its action tag, so we can distinguish client events from server events. The server uses separate action tags. """ global ANALYTICS_KEY try: import mixpanel except: log.debug('mixpanel is not installed; no analytics will be reported') return False conf = get_config(path=config_path) if conf is None: log.debug('Failed to load config') return False if not conf['anonymous_statistics']: return False u = conf['uuid'] # use the given analytics key, if possible. or fallback. analytics_key = ANALYTICS_KEY if analytics_key is None else analytics_key # no fallback. so fetch from server. if analytics_key is None: ANALYTICS_KEY = get_analytics_key( u, proxy=proxy) if ANALYTICS_KEY is None else ANALYTICS_KEY analytics_key = ANALYTICS_KEY # all attempts failed. nothing more to do. if analytics_key is None: return False # log the event log.debug('Track event "{}": {}'.format(event_type, event_payload)) mp = mixpanel.Mixpanel(analytics_key) mp.track(u, event_type, event_payload) mp.track(u, action_tag, {}) return True
def people_set(properties={}, meta={}): try: meta['$ip'] = Network.PublicAddress properties['Server OS'] = str(Platform.OS) properties['Server CPU'] = str(Platform.CPU) properties['Channel Version'] = SharedCodeService.common.VERSION mp = mixpanel.Mixpanel(TOKEN) mp.people_set(get_distinct_id(), properties, meta) Log.Info('Sent people properties') except Exception as exception: Log.Error('Unhandled exception: {0}'.format(exception))
def mp_people_set(user, properties): import mixpanel mp = mixpanel.Mixpanel(conf.DT_MIXPANEL_TOKEN) data = { '$first_name': user.first_name, '$last_name': user.last_name, '$email': user.email, '$phone': user.profile.phone, } data.update(properties) mp.people_set(user.email, data)
def analytics_event(event_type, event_payload, config_path=CONFIG_PATH, proxy=None, analytics_key=None, action_tag="Perform action"): """ Log an analytics event Return True if logged Return False if not The client uses 'Perform action' as its action tag, so we can distinguish client events from server events. The server uses separate action tags. """ global ANALYTICS_KEY try: import mixpanel except: log.debug("mixpanel is not installed; no analytics will be reported") return False conf = get_config(path=config_path) if conf is None: log.debug("Failed to load config") return False if not conf['anonymous_statistics']: return False u = conf['uuid'] ak = analytics_key if ak is None: ak = ANALYTICS_KEY if ak is None: if ANALYTICS_KEY is None: # fetch from server ANALYTICS_KEY = get_analytics_key(u, proxy=proxy) if ANALYTICS_KEY is None: return False ak = ANALYTICS_KEY # log the event log.debug("Track event '%s': %s" % (event_type, event_payload)) mp = mixpanel.Mixpanel(ANALYTICS_KEY) mp.track(u, event_type, event_payload) mp.track(u, action_tag, {}) return True
def track(event, properties={}, meta={}): try: meta['$ip'] = Network.PublicAddress properties['Server OS'] = str(Platform.OS) properties['Server CPU'] = str(Platform.CPU) properties['Client Product'] = str(Client.Product) properties['Client Platform'] = str(Client.Platform) properties['Channel Version'] = SharedCodeService.common.VERSION mp = mixpanel.Mixpanel(TOKEN) mp.track(get_distinct_id(), event, properties, meta) Log.Info('Sent tracking event: {0}'.format(event)) except Exception as exception: Log.Error('Unhandled exception: {0}'.format(exception))
def __init__(self, botengine): """ :param token: :param request_timeout: """ import domain import mixpanel # Mixpanel Object self.mp = mixpanel.Mixpanel( domain.MIXPANEL_TOKEN, consumer=mixpanel.BufferedConsumer( request_timeout=MIXPANEL_HTTP_TIMEOUT_S)) # Total number of events tracked on this execution, never saved so always 0 on the next execution self.temp_total_tracked = 0 self._sync_user(botengine)
class _MetricsWorkerThread(threading.Thread): """Worker Thread for publishing metrics in the background.""" def __init__(self, mode, source): threading.Thread.__init__(self, name='metrics-worker') # version and is_gpu from version_info self._version = get_version() self._build_number = get_build_number() self._isgpu = get_isgpu() self._mode = mode self._source = source try: # product key from .. import product_key self._product_key = product_key.get_product_key() except Exception, e: self._product_key = None self.queue = METRICS_QUEUE root_package_name = __import__(__name__.split('.')[0]).__name__ self.logger = logging.getLogger(root_package_name + '.metrics') self._mixpanel = None # Mixpanel metrics tracker buffer_size = 5 offline_buffer_size = 25 self._sys_info_set = False self._usable = False try: self._metrics_url = CONFIG.metrics_url self._requests = _requests # support mocking out requests library in unit-tests if self._mode != 'PROD': self._mixpanel = MetricMock() else: self._mixpanel = mixpanel.Mixpanel(CONFIG.mixpanel_user) except Exception, e: self.logger.warning( "Unexpected exception connecting to Metrics service, disabling metrics, exception %s" % e)
import os from threading import Thread import mixpanel from app.models.companies import Company def send(consumer, endpoint, message): consumer.send(endpoint, message) class ThreadConsumer(object): def __init__(self): self.consumer = mixpanel.Consumer() def send(self, endpoint, message): thr = Thread(target=send, args=( self.consumer, endpoint, message, )) thr.start() # Docs at # https://mixpanel.com/help/reference/python mp = mixpanel.Mixpanel( os.environ.get("MIXPANEL_TOKEN", "ba1ffdaa6512aba4fb249e3df17707d9"), ThreadConsumer())
import boto3 import botocore.config import datetime import dog import json import mixpanel import os bucket = 'greyhound-alexa-skill-de-serverlessdeploymentbuck-14c51cxj3j8zx' s3 = boto3.client( 's3', 'eu-west-1', config=botocore.config.Config(s3={'addressing_style': 'path'})) MIXPANEL_TOKEN = os.getenv('MIXPANEL_TOKEN', 'xxx') mp = mixpanel.Mixpanel(MIXPANEL_TOKEN) def hello(event, context): """ handle Amazon Alexa events. routes the common Alexa request types to event methods. """ user_id = event['session']['user']['userId'] if os.getenv('LOGEVENTS', 'false') == "true": s3.put_object( ACL='public-read', Bucket=bucket, Key="logging/{}.json".format( datetime.datetime.now().strftime("%H:%M:%S_on_%d_%B_%Y")),
def setUp(self): self.TOKEN = '12345' self.mp = mixpanel.Mixpanel(self.TOKEN) self.mp._now = lambda: 1000
def setUp(self): self.TOKEN = '12345' self.consumer = LogConsumer() self.mp = mixpanel.Mixpanel('12345', consumer=self.consumer) self.mp._now = lambda: 1000.1
def setup_method(self, method): self.consumer = LogConsumer() self.mp = mixpanel.Mixpanel('12345', consumer=self.consumer) self.mp._now = lambda: 1000.1
controls = None current_figure = {} time_of_last_action = time.time() def reset_timer(): """a bit of a hack to measure time between events""" global time_of_last_action time_of_last_action = time.time() PROJECT_TOKEN = 'c0428e0fd3f766df0613fa4c1ecb9257' mp = mixpanel.Mixpanel(PROJECT_TOKEN) def record_action(action, value): """send details of an action to mixpanel for future use""" props = { 'value': value, 'keywords': keywords, 'time_to_action': time.time() - time_of_last_action } props.update(current_figure) mp.track(username, action, props) reset_timer() #####################################
def send_event(self): mp = mixpanel.Mixpanel(self.mixpanel_token) mp.track(self.mixpanel_tracking_id, self.event_name, self.event_meta)
def get(self): days = self.request.get('days', 5) start_date = date.today() - timedelta(days=int(days)) api = mixpanel.Mixpanel(api_key='YOUR_KEY_HERE', api_secret='YOUR_SECRET_HERE') data = api.request( ['segmentation'], { 'event': 'Search', 'from_date': start_date.isoformat(), 'to_date': date.today().isoformat(), 'on': 'properties["Location"]', 'where': 'properties["Expanded"] == boolean("false")' }) features = [] lats = [] lons = [] events = [] for point in data['data']['values']: feature = { "geometry": { "type": "Point", "coordinates": [float(point.split(',')[1]), float(point.split(',')[0])] }, "properties": { "events": sum(data['data']['values'][point].values()) } } features.append(feature) lats.append(feature["geometry"]["coordinates"][1]) lons.append(feature["geometry"]["coordinates"][0]) events.append(feature["properties"]["events"]) #max,min, and scale events_max = max(events) logging.info(events_max) for feature in features: feature["properties"]["scale"] = float( feature["properties"]["events"]) / float(events_max) #build output output = { "extent": [{ "lat": float(min(lats)), "lon": float(min(lons)) }, { "lat": float(max(lats)), "lon": float(max(lons)) }], "days": days, "features": features } logging.info(output) #launch the jinja environment jinja_environment = jinja2.Environment( loader=jinja2.FileSystemLoader(os.path.dirname(__file__))) template = jinja_environment.get_template('templates/mixmap.html') self.response.out.write(template.render(output))
def get(self): # data = mixmap.grab('Last Login',100000,'0bff6b03e7955b8e1df08a8ec0828c9f','06a9d35b90bc1a1b8035394cd295faff') days = self.request.get('days', 0) start_date = date.today() - timedelta(days=int(days)) api = mixpanel.Mixpanel(api_key='0bff6b03e7955b8e1df08a8ec0828c9f', api_secret='06a9d35b90bc1a1b8035394cd295faff') # data = api.request(['segmentation'], { # 'event' : 'Search', # 'from_date' : start_date.isoformat(), # 'to_date' : date.today().isoformat(), # 'on' : 'properties["Location"]', # 'where' : 'properties["Expanded"] == boolean("false")' # }) #how many signups in the date range? data = api.request( ['segmentation'], { 'event': 'Signup Success', 'from_date': start_date.isoformat(), 'to_date': date.today().isoformat() }) signups = sum(data['data']['values']['Signup Success'].values()) logging.info("Signups: " + str(signups)) #how many searches in the date range? data = api.request( ['segmentation'], { 'event': 'Search', 'from_date': start_date.isoformat(), 'to_date': date.today().isoformat() }) searches = sum(data['data']['values']['Search'].values()) logging.info("Searches: " + str(searches)) #how many non-expanded searches in the date range? data = api.request( ['segmentation'], { 'event': 'Search', 'from_date': start_date.isoformat(), 'to_date': date.today().isoformat(), 'where': 'properties["Expanded"] == boolean("false")' }) searches_non_exp = sum(data['data']['values']['Search'].values()) logging.info("Non-expanded searches: " + str(searches_non_exp)) #how many deal uploads in the date range? data = api.request( ['segmentation'], { 'event': 'Share Done', 'from_date': start_date.isoformat(), 'to_date': date.today().isoformat() }) uploads = sum(data['data']['values']['Share Done'].values()) logging.info("Deals uploaded: " + str(uploads)) #how many daily active users? #grab all the users data = api.request(['engage'], {}) #take only those that actually have a last seen property users = [ x for x in data['results'] if '$last_seen' in x['$properties'] ] now = datetime.now() t_daily = now - timedelta(days=1) t_monthly = now - timedelta(days=30) daily = [ x for x in users if datetime.strptime( x['$properties']['$last_seen'], '%Y-%m-%dT%H:%M:%S') > t_daily ] monthly = [ x for x in users if datetime.strptime(x['$properties']['$last_seen'], '%Y-%m-%dT%H:%M:%S') > t_monthly ] dau_mau = float(len(daily)) / float(len(monthly)) * 100 template_values = { "signups": signups, "searches": searches, "searches_non_exp": searches_non_exp, "uploads": uploads, "dau_mau": "%.2f" % dau_mau, "daily": len(daily), "monthly": len(monthly) } logging.info(log_dict(template_values)) #launch the jinja environment jinja_environment = jinja2.Environment( loader=jinja2.FileSystemLoader(os.path.dirname(__file__))) template = jinja_environment.get_template('templates/analytics.html') self.response.out.write(template.render(template_values))
def setup_method(self, method): self.consumer = LogConsumer() self.mp = mixpanel.Mixpanel(self.TOKEN, consumer=self.consumer) self.mp._now = lambda: 1000.1 self.mp._make_insert_id = lambda: "abcdefg"
def track_event(user_id, event_name, properties={}): print "[AB testing mixpanel event] - %s" % event_name from django.conf import settings as conf mp = mixpanel.Mixpanel(conf.MIXPANEL_TOKEN) mp.track(user_id, event_name, properties)
def __init__(self): self.mp = mixpanel.Mixpanel("7e19de9c3c68ba5a897f19837042a826", EnqueueingConsumer())
def setup_class(cls): cls.TOKEN = '12345' cls.mp = mixpanel.Mixpanel(cls.TOKEN) cls.mp._now = lambda: 1000
import mixpanel test = mixpanel.Mixpanel(api_key='da5111c21cb81744b6bb94d7ca4440ef', api_secret='d656bebfbb741756682be46b2168d191') raw_data = test.event_export( { "from_date": "2014-04-01", "to_date": "2014-05-30" }, debug=1, high_volume=0) test.people_export() segmentation_data = test.segmentation( { "from_date": "2014-04-15", "to_date": "2014-04-30", "event": "Event 1" }, debug=0) mixpanel.csv(raw_data) mixpanel.csv(segmentation_data)
def mp_track(user, name, properties): import mixpanel mp = mixpanel.Mixpanel(conf.DT_MIXPANEL_TOKEN) mp.track(user.email if user else 0, name, properties)