Beispiel #1
0
    def test_rows(self):
        def positive_int(n):
            if not n or n < 0:
                return
            return int(n)

        t = Table([
            Column('foo', visible=1),
            Column('bar', visible=0),
            Column('baz', type_cast=positive_int, average=100),
        ])

        data = [
            (9999, '1', 123),
            (123, '2', 1234),
            (0000, '3', 23),
            (123, '4', 123),
            (123, '5', 0),
        ]

        for d in data:
            t.add(d)

        self.assertEqual(len(t.rows), len(data))
        self.assertEqual(t.get('foo').min_row, (None, None))
        self.assertEqual(t.get('bar').max_row, (None, None))
        self.assertEqual(t.get('baz').min_row[0], 23)
        self.assertEqual(t.get('baz').max_row[0], 1234)

        rows = t.iter_visible()
        self.assertEqual(list(next(rows)), [t.columns[1], t.columns[0]])
        self.assertEqual(list(next(rows)), ['1', 9999])

        t.tag_rows()
        self.assertFalse(t.rows[-1].tags)
Beispiel #2
0
    def _get_geo(self, google_query, summary_metrics):
        t = google_query.get_table(
            params={
                'ids': 'ga:%s' % self.remote_id,
                'start-date': self.date_start,
                'end-date': self.date_end,
                'sort': '-ga:users',
                'max-results': '50',
            },
            dimensions=[
                Column('ga:country',
                       label='Country',
                       visible=1,
                       type_cast=_prune_abstract),
            ],
            metrics=[col.new() for col in summary_metrics] + [
                Column('ga:sessionsPerUser',
                       label='Sessions Per User',
                       threshold=0.0,
                       type_cast=float,
                       type_format=_format_float),
            ],
        )
        total = float(t.get('ga:users').sum)
        out = Table(columns=[col.new() for col in t.columns] + [
            Column(
                'users', label='Users', visible=0,
                type_format=_format_percent),
        ])
        out.get('ga:users')._threshold = None
        out.set_visible('users', 'ga:country')
        idx_users = t.column_to_index['ga:users']
        for row in t.rows[:5]:
            out.add(row.values + [row.values[idx_users] * 100.0 / total])

        out.tag_rows()

        return out
Beispiel #3
0
    def _get_goals(self, google_query, interval_field):
        goals_api = 'https://www.googleapis.com/analytics/v3/management/accounts/{accountId}/webproperties/{webPropertyId}/profiles/{profileId}/goals'
        r = google_query.get(
            goals_api.format(profileId=self.remote_data['id'],
                             **self.remote_data))
        has_goals = r.get('items') or []
        goal_metrics = [
            Column('ga:goal{id}Completions'.format(id=g['id']),
                   label=g['name'],
                   type_cast=float) for g in has_goals if g.get('active')
        ]

        if not goal_metrics:
            return

        # Note: max 10 metrics allowed
        metrics = goal_metrics[-9:] + [Column('ga:sessions', type_cast=int)]
        raw_table = google_query.get_table(
            params={
                'ids': 'ga:%s' % self.remote_id,
                'start-date': self.previous_date_start,  # Extra week
                'end-date': self.date_end,
                'sort': '-{}'.format(interval_field),
            },
            metrics=metrics,
            dimensions=[
                Column(interval_field),
            ],
        )

        if len(raw_table.rows) != 2:
            # Less than 2 weeks of data available
            return

        t = Table(columns=[
            Column('goal', label='Goals', visible=1, type_cast=_cast_title),
            Column('completions',
                   label='Events',
                   visible=0,
                   type_cast=int,
                   type_format=h.human_int,
                   threshold=0),
        ])

        num_sessions, num_sessions_last = [
            next(v) for v in raw_table.iter_rows('ga:sessions')
        ]

        this_week, last_week = raw_table.rows
        col_compare = t.get('completions')
        col_compare_delta = Column('%s:delta' % col_compare.id,
                                   label='Events',
                                   type_cast=float,
                                   type_format=h.human_delta,
                                   threshold=0)
        has_completions = False
        for col_id, pos in raw_table.column_to_index.items():
            col = raw_table.columns[pos]
            if not col.id.startswith('ga:goal'):
                continue

            completions, completions_last = this_week.values[
                pos], last_week.values[pos]
            percent_completions = completions * 100.0 / num_sessions if num_sessions else 0.0
            percent_completions_last = completions_last * 100.0 / num_sessions_last if num_sessions_last else 0.0
            row = t.add([col.label, completions])
            if not row:
                # Boring
                continue

            if completions > 0:
                row.tag(type="Conversion",
                        value=_format_percent(percent_completions))

            if completions + completions_last > 0:
                has_completions = True
                # Old method:
                # delta = (percent_completions - percent_completions_last) / 100.0
                # New method (same as GA shows):
                delta = completions / completions_last - 1 if completions_last > 0.0 else 1.0
                if abs(delta) > 0.001:
                    row.tag(type='delta',
                            value=delta,
                            column=col_compare_delta,
                            is_positive=delta > 0)

        if not has_completions:
            return

        t.sort(reverse=True)

        return t