Example #1
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        if not self.request.user.has_adwords_account:
            return context

        context['has_adwords_account'] = True

        date_range_form = DateRangeForm()
        date_from = date_range_form.fields['date_from'].initial()
        date_to = date_range_form.fields['date_to'].initial()
        should_aggregate = date_range_form.fields['should_aggregate'].initial

        if 'date_from' in self.request.GET and 'date_to' in self.request.GET:
            date_range_form = DateRangeForm(self.request.GET.copy())
            if date_range_form.is_valid():
                date_from = date_range_form.cleaned_data['date_from']
                date_to = date_range_form.cleaned_data['date_to']
                should_aggregate = date_range_form.cleaned_data[
                    'should_aggregate']

        context['date_range_form'] = date_range_form
        context['chart_range'] = self.request.GET.get('chart_range',
                                                      'thisWeek')

        adapter = Adapter(self.request.user)
        kwargs = {
            'cast_dates': False,
            'campaign_id': self.object.adwords_campaign_id
        }
        if context['chart_range'] != 'allTime':
            kwargs['date_range'] = adapter.format_date_range(
                date_from, date_to)

        metrics = adapter.get_campaign_metrics(**kwargs)

        if should_aggregate:
            metrics = adapter.aggregate_campaign_metrics_to_monthly(
                metrics, date_format='%Y-%m-%d')

        for metric in metrics.values():
            metric['cpc'] /= 10**6

        context['metrics'] = simplejson.dumps(metrics)

        return context
Example #2
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        if not self.request.user.has_adwords_account:
            return context

        context['has_adwords_account'] = True

        date_range_form = DateRangeForm()
        date_from = date_range_form.fields['date_from'].initial()
        date_to = date_range_form.fields['date_to'].initial()
        should_aggregate = date_range_form.fields['should_aggregate'].initial

        if 'date_from' in self.request.GET and 'date_to' in self.request.GET:
            date_range_form = DateRangeForm(self.request.GET.copy())
            if date_range_form.is_valid():
                date_from = date_range_form.cleaned_data['date_from']
                date_to = date_range_form.cleaned_data['date_to']
                should_aggregate = date_range_form.cleaned_data[
                    'should_aggregate']

        context['date_range_form'] = date_range_form
        context['chart_range'] = self.request.GET.get('chart_range',
                                                      'last30Days')

        adapter = Adapter(self.request.user)
        kwargs = {'cast_dates': False}
        if context['chart_range'] != 'allTime':
            kwargs['date_range'] = adapter.format_date_range(
                date_from, date_to)

        metrics = adapter.get_campaign_metrics(**kwargs)

        if should_aggregate:
            metrics = adapter.aggregate_campaign_metrics_to_monthly(
                metrics, date_format='%Y-%m-%d')

        for metric in metrics.values():
            metric['cpc'] /= 10**6
        context['metrics'] = simplejson.dumps(metrics)

        event_date_range = self.request.GET.get('range', 'today').lower()
        date_range_lengths = {'today': 1, 'week': 7, 'month': 28}

        with connection.cursor() as cursor:
            cursor.execute(
                "SELECT campaign_modifiers_keywordevent.action, COUNT(*) FROM campaign_modifiers_keywordevent INNER JOIN campaign_modifiers_modifierprocesslog ON campaign_modifiers_keywordevent.modifier_process_log_id=campaign_modifiers_modifierprocesslog.id INNER JOIN reports_campaign ON campaign_modifiers_modifierprocesslog.adwords_campaign_id=reports_campaign.adwords_campaign_id WHERE reports_campaign.owner_id = %s AND campaign_modifiers_keywordevent.created_at BETWEEN NOW() - INTERVAL %s DAY AND NOW() GROUP BY captivise.campaign_modifiers_keywordevent.action;",
                [self.request.user.id, date_range_lengths[event_date_range]])
            events = cursor.fetchall()

        increased = 0
        decreased = 0
        unchanged = 0
        paused = 0

        for event in events:
            if event[0] == "increased_cpc":
                increased = event[1]
            if event[0] == "decreased_cpc":
                decreased = event[1]
            if event[0] == "no_action":
                unchanged = event[1]
            if event[0] == "paused":
                paused = event[1]

        context.update({
            'increased_bid_count': increased,
            'decreased_bid_count': decreased,
            'unchanged_bid_count': unchanged,
            'paused_keywords_count': paused,
        })

        return context