Beispiel #1
0
 def __init__(self,
              api_key,
              api_secret,
              timeout=None,
              auth_class=SignatureAuth):
     self.api_key = _totext(api_key)
     self.api_secret = _totext(api_secret)
     self.timeout = timeout
     self.connection = Connection(self)
     self.auth = auth_class(self)
Beispiel #2
0
    def get_export(self, start_date, end_date, event=None, where=None, bucket_id=None, response_format=FORMAT_JSON, result_key=""):
        """
        Get a "raw dump" of tracked events over a time period.

        Yields events as they are returned (matching the format shown below).

        Args:
            `start_date`: [str] The date in yyyy-mm-dd format from which to begin querying for the
                                event from. This date is inclusive.
                          [sample]: "2014-04-01"
            `end_date`: [str] The date in yyyy-mm-dd format from which to stop querying for the
                              event from. This date is inclusive.
                        [sample]: "2014-04-01"
            `event`: [array (optional)]: The event or events that you wish to get data for.
                               [sample]: ["play song", "log in", "add playlist"]
            `where`: [str] An expression to filter events by.
            `bucket_id`: [str] The specific data bucket you would like to query.
            'result_key': [str] The field in the event result that will act as key to compose the dict with the query results

        Event format:
            {"event":"Viewed report","properties":{"distinct_id":"foo","time":1329263748,"origin":"invite",
            "origin_referrer":"http://mixpanel.com/projects/","$initial_referring_domain":"mixpanel.com",
            "$referrer":"https://mixpanel.com/report/3/stream/","$initial_referrer":"http://mixpanel.com/",
            "$referring_domain":"mixpanel.com","$os":"Linux","origin_domain":"mixpanel.com","tab":"stream",
            "$browser":"Chrome","Project ID":"3","mp_country_code":"US"}}
        """
        start_date_obj = self._validate_date(start_date)
        end_date_obj = self._validate_date(end_date)

        # Check the actual dates
        if start_date_obj > end_date_obj:
            raise exceptions.InvalidDateException('The `start_date` specified after the `end_date`; you will not receive any events.')

        if isinstance(event, str):
            event = [event]

        response = self.connection.raw_request(
            Connection.DATA_ENDPOINT,
            'export',
            {
                'from_date': start_date,
                'to_date': end_date,
                'event': event,
                'where': where,
                'bucket': bucket_id,
            },
            response_format
        )
        result = {}
        for line in response:
            # print line
            # result.append(json.loads(_totext(line)))
            # NOTA: Este es un pequeno cambio enfocado al uso que se le da este modulo para la metrica de latencia
            event = json.loads(_totext(line))
            key = event['properties'][result_key]
            # print key
            result[key] = event['properties'] 
            # print result[key]
        return result
Beispiel #3
0
 def authenticate(self, url, params):
     """
     returns a request object ready to be issued to the Mixpanel API
     """
     request_url = '{base_url}?{encoded_params}'.format(
         base_url=url,
         encoded_params=_unicode_urlencode(params)
     )
     request_headers = {
         'Authorization': 'Basic ' + _totext(base64.standard_b64encode(_tobytes("{}:".format(self.client.api_secret))))
     }
     return url_request.Request(request_url, headers=request_headers)
Beispiel #4
0
 def authenticate(self, url, params):
     """
     returns a request object ready to be issued to the Mixpanel API
     """
     request_url = '{base_url}?{encoded_params}'.format(
         base_url=url, encoded_params=_unicode_urlencode(params))
     request_headers = {
         'Authorization':
         'Basic ' + _totext(
             base64.standard_b64encode(
                 _tobytes("{}:".format(self.client.api_secret))))
     }
     return url_request.Request(request_url, headers=request_headers)
Beispiel #5
0
 def __init__(self, api_key, api_secret, timeout=None):
     self.api_key = _totext(api_key)
     self.api_secret = _totext(api_secret)
     self.timeout = timeout
Beispiel #6
0
 def __init__(self, api_key, api_secret, timeout=None):
     self.api_key = _totext(api_key)
     self.api_secret = _totext(api_secret)
     self.timeout = timeout
     self.connection = Connection(self)
Beispiel #7
0
    def get_export(self, start_date, end_date, event=None, where=None, bucket_id=None, response_format=FORMAT_JSON):
        """
        Get a "raw dump" of tracked events over a time period.

        Yields events as they are returned (matching the format shown below).

        Args:
            `start_date`: [str] The date in yyyy-mm-dd format from which to begin querying for the
                                event from. This date is inclusive.
                          [sample]: "2014-04-01"
            `end_date`: [str] The date in yyyy-mm-dd format from which to stop querying for the
                              event from. This date is inclusive.
                        [sample]: "2014-04-01"
            `event`: [array (optional)]: The event or events that you wish to get data for.
                               [sample]: ["play song", "log in", "add playlist"]
            `where`: [str] An expression to filter events by.
            `bucket_id`: [str] The specific data bucket you would like to query.

        Event format:
            {"event":"Viewed report","properties":{"distinct_id":"foo","time":1329263748,"origin":"invite",
            "origin_referrer":"http://mixpanel.com/projects/","$initial_referring_domain":"mixpanel.com",
            "$referrer":"https://mixpanel.com/report/3/stream/","$initial_referrer":"http://mixpanel.com/",
            "$referring_domain":"mixpanel.com","$os":"Linux","origin_domain":"mixpanel.com","tab":"stream",
            "$browser":"Chrome","Project ID":"3","mp_country_code":"US"}}
        """
        start_date_obj = self._validate_date(start_date)
        end_date_obj = self._validate_date(end_date)

        # Check the actual dates
        if start_date_obj > end_date_obj:
            raise exceptions.InvalidDateException('The `start_date` specified after the `end_date`; you will not receive any events.')

        # the provided event should be an array even when a singleton
        # if a singleton string/unicode is provided, put it into an array
        if isinstance(event, six.string_types):
            event = [event]

        response = self.connection.raw_request(
            Connection.DATA_ENDPOINT,
            'export',
            {
                'from_date': start_date,
                'to_date': end_date,
                'event': event,
                'where': where,
                'bucket': bucket_id,
            },
            response_format
        )
        # per mixpanel documentation it is necessary to load
        # the response in it's entirety before processing:
        #     > This endpoint uses gzip to compress the transfer;
        #     > as a result, raw exports should not be processed until
        #     > the file is received in its entirety.
        # https://mixpanel.com/docs/api-documentation/exporting-raw-data-you-inserted-into-mixpanel
        response_data = response.read()
        lines = _totext(response_data).split('\n')
        
        for line in lines:
            if line:
                yield json.loads(line)
Beispiel #8
0
 def __init__(self, api_key, api_secret, timeout=None, auth_class=SignatureAuth):
     self.api_key = _totext(api_key)
     self.api_secret = _totext(api_secret)
     self.timeout = timeout
     self.connection = Connection(self)
     self.auth = auth_class(self)
 def __init__(self, api_key, api_secret):
     self.api_key = _totext(api_key)
     self.api_secret = _totext(api_secret)
     self.connection = Connection(self)
Beispiel #10
0
    def get_export(self,
                   start_date,
                   end_date,
                   event=None,
                   where=None,
                   bucket_id=None,
                   response_format=FORMAT_JSON):
        """
        Get a "raw dump" of tracked events over a time period.

        Yields events as they are returned (matching the format shown below).

        Args:
            `start_date`: [str] The date in yyyy-mm-dd format from which to begin querying for the
                                event from. This date is inclusive.
                          [sample]: "2014-04-01"
            `end_date`: [str] The date in yyyy-mm-dd format from which to stop querying for the
                              event from. This date is inclusive.
                        [sample]: "2014-04-01"
            `event`: [array (optional)]: The event or events that you wish to get data for.
                               [sample]: ["play song", "log in", "add playlist"]
            `where`: [str] An expression to filter events by.
            `bucket_id`: [str] The specific data bucket you would like to query.

        Event format:
            {"event":"Viewed report","properties":{"distinct_id":"foo","time":1329263748,"origin":"invite",
            "origin_referrer":"http://mixpanel.com/projects/","$initial_referring_domain":"mixpanel.com",
            "$referrer":"https://mixpanel.com/report/3/stream/","$initial_referrer":"http://mixpanel.com/",
            "$referring_domain":"mixpanel.com","$os":"Linux","origin_domain":"mixpanel.com","tab":"stream",
            "$browser":"Chrome","Project ID":"3","mp_country_code":"US"}}
        """
        start_date_obj = self._validate_date(start_date)
        end_date_obj = self._validate_date(end_date)

        # Check the actual dates
        if start_date_obj > end_date_obj:
            raise exceptions.InvalidDateException(
                'The `start_date` specified after the `end_date`; you will not receive any events.'
            )

        # the provided event should be an array even when a singleton
        # if a singleton string/unicode is provided, put it into an array
        if isinstance(event, six.string_types):
            event = [event]

        response = self.connection.raw_request(
            Connection.DATA_ENDPOINT, 'export', {
                'from_date': start_date,
                'to_date': end_date,
                'event': event,
                'where': where,
                'bucket': bucket_id,
            }, response_format)
        # per mixpanel documentation it is necessary to load
        # the response in it's entirety before processing:
        #     > This endpoint uses gzip to compress the transfer;
        #     > as a result, raw exports should not be processed until
        #     > the file is received in its entirety.
        # https://mixpanel.com/docs/api-documentation/exporting-raw-data-you-inserted-into-mixpanel
        response_data = response.read()
        lines = _totext(response_data).split('\n')

        for line in lines:
            if line:
                yield json.loads(line)
Beispiel #11
0
 def __init__(self, api_key, api_secret, timeout=None):
     self.api_key = _totext(api_key)
     self.api_secret = _totext(api_secret)
     self.timeout = timeout
     self.connection = Connection(self)