Beispiel #1
0
    def get_linger_data(self, team, slug=None, start_date=None):
        params = self.get_query_params(team=team, slug=slug, start_date=start_date)
        data = GoogleAnalytics.query_ga(params)

        if not data.get("rows"):
            logger.info("No rows found, done.")
            return False

        rows = self.clean_data(data)
        return rows
Beispiel #2
0
    def get_unique_visitor_data(self, team, slug):
        params = self.get_query_params(team=team, slug=slug)
        data = GoogleAnalytics.query_ga(params)
        # import pdb; pdb.set_trace();

        if not data.get("rows"):
            logger.info("No rows found, done.")
            return False

        return data.get("rows")[0][0]
Beispiel #3
0
    def get_unique_visitor_data(self, team, slug):
        params = self.get_query_params(team=team, slug=slug)
        data = GoogleAnalytics.query_ga(params)
        # import pdb; pdb.set_trace();

        if not data.get('rows'):
            logger.info('No rows found, done.')
            return False

        return data.get('rows')[0][0]
Beispiel #4
0
    def get_linger_data(self, team, slug=None, start_date=None):
        params = self.get_query_params(team=team,
                                       slug=slug,
                                       start_date=start_date)
        data = GoogleAnalytics.query_ga(params)

        if not data.get('rows'):
            logger.info('No rows found, done.')
            return False

        rows = self.clean_data(data)
        return rows
Beispiel #5
0
    def median_of_time_buckets(time_buckets):
        """
        Take a list of [seconds, count] tuples and get the median seconds.
        """
        lst = []

        # Flatten the [seconds, count] tuples
        # This is a really bad way to do this!
        # Yuuuuge number of objects created!
        for bucket in time_buckets:
            for _ in range(bucket[1]):
                lst.append(bucket[0])

        median = GoogleAnalytics.median(lst)
        return int(median)
Beispiel #6
0
    def median_of_time_buckets(time_buckets):
        """
        Take a list of [seconds, count] tuples and get the median seconds.
        """
        lst = []

        # Flatten the [seconds, count] tuples
        # This is a really bad way to do this!
        # Yuuuuge number of objects created!
        for bucket in time_buckets:
            for _ in range(bucket[1]):
                lst.append(bucket[0])

        median = GoogleAnalytics.median(lst)
        return int(median)
Beispiel #7
0
    def get_user_data(self, team, start_date=None):
        """
        Get the number of users in the last seven days
        """
        if not start_date:
            start_date = '90daysAgo'

        params = {
            'ids': 'ga:{0}'.format(team['ga_org_id']),
            'start-date': start_date,  # start_date.strftime('%Y-%m-%d'),
            'end-date': 'today',
            'metrics': 'ga:users',
            # 'dimensions': 'ga:eventLabel',
            # 'filters': filters,
            'max-results': app_config.GA_RESULT_SIZE,
            'samplingLevel': app_config.GA_SAMPLING_LEVEL,
            'start-index': 1,
        }

        return GoogleAnalytics.query_ga(params)
Beispiel #8
0
    def get_user_data(self, team, start_date=None):
        """
        Get the number of users in the last seven days
        """
        if not start_date:
            start_date = '90daysAgo'

        params = {
            'ids': 'ga:{0}'.format(team['ga_org_id']),
            'start-date': start_date, # start_date.strftime('%Y-%m-%d'),
            'end-date': 'today',
            'metrics': 'ga:users',
            # 'dimensions': 'ga:eventLabel',
            # 'filters': filters,
            'max-results': app_config.GA_RESULT_SIZE,
            'samplingLevel': app_config.GA_SAMPLING_LEVEL,
            'start-index': 1,
        }

        return GoogleAnalytics.query_ga(params)
    def get_median(self, data):
        """
        Take the scroll depth data we have (number of people per percent)
        Then calculate how many people only got to THAT bucket (aka didn't get
        to the next percent bucket)
        """
        length = len(data)
        for i, row in enumerate(data):
            if not i == length - 1:
                row[1] = row[1] - data[i + 1][1]

        lst = []

        # Flatten the [percent, count] tuples
        # This is a really inefficient way to do this!
        for bucket in data:
            for _ in range(bucket[1]):
                lst.append(bucket[0])

        median = GoogleAnalytics.median(lst)
        return int(median)
    def get_slug_message(self, slug, story=None):
        # Try to match the story to a slug to accurately get a team
        # The Google Analytics property ID comes from the team config
        # We use the default team if none is found
        stories = Story.select().where(Story.slug.contains(slug))
        team = self.config.get_team_for_stories(stories)

        params = self.get_slug_query_params(team=team, slug=slug)
        data = GoogleAnalytics.query_ga(params)
        if not data.get('rows'):
            logger.info('No rows found for slug %s' % slug)
            return

        # Clean up the data
        clean_data = self.clean_data(data.get('rows'))
        total_people = self.get_total_people(clean_data)
        friendly_people = "{:,}".format(total_people) # Comma-separated #s
        median = self.get_median(clean_data)

        # Set up the chart
        scroll_histogram_url = self.get_chart(clean_data)
        if story:
            scroll_histogram_url = ChartTools.add_screenshot_to_chart(story,
                                                                scroll_histogram_url)

        # TODO: Not confident in median calculations so far
        # text = "*%s people* got a median of *%s percent* down the page." % (friendly_people, median)
        text = ''
        attachments = [{
            "fallback": slug + " update",
            "color": "#eeeeee",
            "title": "How far down did people scroll?",
            "image_url": scroll_histogram_url
        }]

        return {
            'text': text,
            'attachments': attachments
        }
Beispiel #11
0
 def test_median(self):
     values = [1, 1, 2, 2, 2, 3, 3]
     median = GoogleAnalytics.median(values)
     self.assertEqual(median, 2)