Exemple #1
0
    def execute(self, context):
        hook = SegmentHook(segment_conn_id=self.segment_conn_id,
                           segment_debug_mode=self.segment_debug_mode)

        self.log.info(
            'Sending track event (%s) for user id: %s with properties: %s',
            self.event, self.user_id, self.properties)

        hook.track(user_id=self.user_id,
                   event=self.event,
                   properties=self.properties)
Exemple #2
0
    def execute(self, context: Dict) -> None:
        hook = SegmentHook(segment_conn_id=self.segment_conn_id, segment_debug_mode=self.segment_debug_mode)

        self.log.info(
            'Sending track event (%s) for user id: %s with properties: %s',
            self.event,
            self.user_id,
            self.properties,
        )

        # pylint: disable=no-member
        hook.track(user_id=self.user_id, event=self.event, properties=self.properties)  # type: ignore
Exemple #3
0
 def __init__(self,
              event,
              csv_file,
              segment_conn_id='segment_default',
              segment_debug_mode=False,
              *args,
              **kwargs):
     super().__init__(*args, **kwargs)
     self.event = event
     self.segment_debug_mode = segment_debug_mode
     self.segment_conn_id = segment_conn_id
     self.csv_file = csv_file
     self.hook = SegmentHook(segment_conn_id=self.segment_conn_id,
                             segment_debug_mode=self.segment_debug_mode)
     self.analytics = self.hook.get_conn()
Exemple #4
0
class SegmentTrackEventOperator(BaseOperator):
    """
    Send Track Event to Segment for a specified user_id and event

    :param csv_file: a CSV (plain or zipped) path with data to be used as event properties.
        One of them *must* be called 'userId' (templated)
    :type csv_file: str
    :param event: The name of the event you're tracking. (templated)
    :type event: str
    :param segment_conn_id: The connection ID to use when connecting to Segment.
    :type segment_conn_id: str
    :param segment_debug_mode: Determines whether Segment should run in debug mode.
        Defaults to False
    :type segment_debug_mode: bool
    """
    template_fields = ('csv_file', 'event')
    ui_color = '#ffd700'

    @apply_defaults
    def __init__(self,
                 event,
                 csv_file,
                 segment_conn_id='segment_default',
                 segment_debug_mode=False,
                 *args,
                 **kwargs):
        super().__init__(*args, **kwargs)
        self.event = event
        self.segment_debug_mode = segment_debug_mode
        self.segment_conn_id = segment_conn_id
        self.csv_file = csv_file
        self.hook = SegmentHook(segment_conn_id=self.segment_conn_id,
                                segment_debug_mode=self.segment_debug_mode)
        self.analytics = self.hook.get_conn()

    def execute(self, context):

        if self.csv_file.endswith('.gz'):
            file_reader = gzip.open(self.csv_file, "rt", newline="")
        else:
            file_reader = open(self.csv_file, 'r')

        csv_reader = csv.DictReader(file_reader)
        for row in csv_reader:
            # converts a csv row into a map: header1 -> value, header2 -> value...
            props = dict(row)
            user_id = props.pop('userId', None)

            if user_id is None:
                self.log.info('No userId set in CSV row: %s >>> Skipping.',
                              props)
                continue

            # fixing numerics types set as strings from csv
            clean_props = dict()
            for key in props:
                clean_props[key] = retype(props.get(key))

            self.log.info(
                'Sending track event (%s) for userId %s with properties: %s',
                self.event, user_id, clean_props)

            self.analytics.track(user_id=user_id,
                                 event=self.event,
                                 properties=clean_props)