class SegmentEventService():
    def __init__(self):
        self.segment = Client(ENV["segment"]["write_key"], 
            debug=True, on_error=self.on_error, send=True, max_queue_size=100000)
        '''
        analytics.write_key = ENV["segment"]["write_key"]
        analytics.debug = True
        analytics.on_error = self.on_error
        '''

    def on_error(self, error, items):
        print("An error occurred:", error)

    #
    # insert an event log
    #
    def insert_event(self, event_log):
        try:
            #print "%s did event %s " % (event_log["anonymousId"], event_log["event"])
            #analytics.track(event_log["userId"], event_log["event"], {}, 
            self.segment.track(event_log["userId"], event_log["event"], {}, 
                event_log["context"], event_log["timestamp"], event_log["anonymousId"],
                event_log["integrations"])
        except Exception, e:
            log.exception(e) 
def track():
    write_key = request.args.get('writeKey')
    params = request.get_json(True)
    
    analytics = Client(write_key)
    analytics.track(params['userId'], params['event'], params['properties'])
    analytics.flush()
    
    return json.dumps({'status': 'success'})
def alias():
    write_key = request.args.get('writeKey')
    params = request.get_json(True)
    
    analytics = Client(write_key)
    analytics.alias(params['previousId'], params['userId'])
    analytics.flush()
    
    return json.dumps({'status': 'success'})
Exemplo n.º 4
0
    def __init__(self, write_key: str):
        is_local = not in_gcp()

        # When `send` is set to False, we do not send any logs to Segment.
        # We also set `debug` to True locally for more logging during development.
        self.client = Client(
            write_key,
            send=(not is_local),
            debug=is_local,
        )
def identify():
    write_key = request.args.get('writeKey')
    params = request.get_json(True)
    
    # Shouldn't have any concurrency issue since we are not multi-threading here
    analytics = Client(write_key)
    analytics.identify(params['userId'], params['traits'])
    analytics.flush()
    
    return json.dumps({'status': 'success'})
Exemplo n.º 6
0
class SegmentClient:
    """Implements base Segment client that can be extended for domain-specific
    use-cases."""
    def __init__(self, write_key: str):
        is_local = not in_gcp()

        # When `send` is set to False, we do not send any logs to Segment.
        # We also set `debug` to True locally for more logging during development.
        self.client = Client(
            write_key,
            send=(not is_local),
            debug=is_local,
        )

    def track(self, user_id: Optional[str], event_name: str,
              metadata: Dict[str, Any]) -> None:
        if user_id:
            self.client.track(user_id, event_name, metadata)
Exemplo n.º 7
0
def upload_to_segment():
    dir_path = dirname(realpath(__file__))
    audit_file_path = join(dir_path, "auditing")
    client = Client(config.SEGMENT_API_KEY)
    for txt_file in [
            join(audit_file_path, filename)
            for filename in listdir(audit_file_path)
            if filename.endswith(".txt")
    ]:
        renamed_to = txt_file.replace(".txt", ".toupload")
        rename(txt_file, renamed_to)
        with open(renamed_to, "rb") as output_file:
            for line in output_file.readlines():
                properties = json.loads(line)
                user = properties.pop("user")
                event = properties.pop("event")
                timestamp = parse(properties.pop("timestamp"))
                kwargs = {
                    "user_id": user,
                    "event": event,
                    "timestamp": timestamp,
                    "properties": properties,
                }
                client.track(**kwargs)
                client.flush()
        afterwards_to = renamed_to.replace(
            ".toupload",
            datetime.now().strftime(".%Y%m%d%H%M"))
        rename(renamed_to, afterwards_to)
Exemplo n.º 8
0
def alias():
    write_key = request.args.get('writeKey')
    params = request.get_json(True)

    analytics = Client(write_key)
    analytics.alias(params['previousId'], params['userId'])
    analytics.flush()

    return json.dumps({'status': 'success'})
Exemplo n.º 9
0
def track():
    write_key = request.args.get('writeKey')
    params = request.get_json(True)

    analytics = Client(write_key)
    analytics.track(params['userId'], params['event'], params['properties'])
    analytics.flush()

    return json.dumps({'status': 'success'})
Exemplo n.º 10
0
def identify():
    write_key = request.args.get('writeKey')
    params = request.get_json(True)

    # Shouldn't have any concurrency issue since we are not multi-threading here
    analytics = Client(write_key)
    analytics.identify(params['userId'], params['traits'])
    analytics.flush()

    return json.dumps({'status': 'success'})
Exemplo n.º 11
0
def setup_analytics():
    global default_client, development

    write_key = getattr(settings, 'ANALYTICS_WRITE_KEY', '')
    host = getattr(settings, 'ANALYTICS_HOST', 'https://api.segment.io')

    production = getattr(settings, 'ENVIRONMENT_NAME', None) == 'production'
    send = not (development
                or getattr(settings, 'TESTING', False)) or production

    if not default_client:
        default_client = Client(write_key=write_key,
                                host=host,
                                debug=False,
                                on_error=handle_analytics_error,
                                send=send)
Exemplo n.º 12
0
def setup_analytics():
    global default_client, development

    write_key = getattr(settings, "ANALYTICS_WRITE_KEY", "")
    host = getattr(settings, "ANALYTICS_HOST", "https://api.segment.io")

    production = getattr(settings, "ENVIRONMENT_NAME", None) == "production"
    send = not (development or getattr(settings, "TESTING", False)) or production

    if not default_client:
        default_client = Client(
            write_key=write_key,
            host=host,
            debug=False,
            on_error=handle_analytics_error,
            send=send,
        )
 def __init__(self):
     self.segment = Client(ENV["segment"]["write_key"], 
         debug=True, on_error=self.on_error, send=True, max_queue_size=100000)
     '''
Exemplo n.º 14
0
import os
import analytics
import pandas as pd
from analytics import Client
from google.cloud import automl_v1beta1
import stacklogging

logger = stacklogging.getLogger(__name__)

MODEL_NAME = "trial_prediction_model"
MODEL_VERSION = "v6"

segment_client = Client(os.getenv("SEGMENT_WRITE_KEY"), max_queue_size=100000)
automl_client = automl_v1beta1.TablesClient(project="buffer-data", region="us-central1")

# Run the batch predict
logger.info("Running AutoML Batch predict")

operation = automl_client.batch_predict(
    bigquery_input_uri="bq://buffer-data.dbt_buffer.predict_publish_trial_conversion_prediction",
    gcs_output_uri_prefix="gs://automl-predictions/",
    model_display_name=f"{MODEL_NAME}_{MODEL_VERSION}",
)

operation.result()
logger.info("AutoML Batch prediction finished")

predicted_at = str(pd.Timestamp.now())

gcs_directory = (
    operation.metadata.batch_predict_details.output_info.gcs_output_directory
Exemplo n.º 15
0
import config
from analytics import Client

segment_client = Client(config.SEGMENT_API_KEY, sync_mode=True)


def _annotation_event(user, action, collection, document, label_type_id, *args,
                      **kwargs):
    properties = {
        'collection': collection,
        'document': document,
        'label_type_id': label_type_id,
    }

    segment_client.track(user_id=user, event=action, properties=properties)


def _login_event(user, *args, **kwargs):
    segment_client.identify(user_id=user)


event_mapper = {'login': _login_event}


class AuditLog:
    def log_event(user, action, *args, **kwargs):
        if action in event_mapper:
            event_mapper[action](user=user, action=action, *args, **kwargs)
        else:
            _annotation_event(user=user, action=action, *args, **kwargs)
Exemplo n.º 16
0

def on_error(error, items):
    print("An error occurred:", error)


writeKey = os.getenv('writeKey', None)
events = os.getenv('events', 500000)

assert writeKey is not None, "Please configure a write key using the writeKey environment variable"

print(f'Sending {events} events to write key "{writeKey}"')

analytics = Client(writeKey,
                   debug=True,
                   on_error=on_error,
                   max_queue_size=math.inf,
                   upload_size=math.inf,
                   upload_interval=1)

start = time.time()

for it in range(events):
    sys.stdout.write(f"\rProgress: {round(it / events * 100, 1)}%")
    analytics.track('test', f'Iterated-{it}', {'plan': it})

print()
print('Shutting down..')
analytics.shutdown()

elapsed = time.time() - start