def test_search_ctr(self, _build_request): """Test googleanalytics.search_ctr().""" execute = _build_request.return_value.get.return_value.execute execute.return_value = SEARCH_CTR_RESPONSE ctr = googleanalytics.search_ctr(date(2013, 6, 6), date(2013, 6, 6)) eq_(1, len(ctr)) eq_(74.88925980111263, ctr['2013-06-06'])
def test_search_ctr(self, _build_request): """Test googleanalytics.search_ctr().""" execute = _build_request.return_value.get.return_value.execute execute.return_value = SEARCH_CTR_RESPONSE ctr = googleanalytics.search_ctr( date(2013, 6, 6), date(2013, 6, 6)) eq_(1, len(ctr)) eq_(74.88925980111263, ctr['2013-06-06'])
def handle(self, **options): if settings.STAGE: # Let's be nice to GA and skip on stage. return # Start updating the day after the last updated. latest_metric = utils._get_latest_metric(SEARCH_CLICKS_METRIC_CODE) if latest_metric is not None: latest_metric_date = latest_metric.start else: latest_metric_date = date(2011, 1, 1) start = latest_metric_date + timedelta(days=1) # Collect up until yesterday end = date.today() - timedelta(days=1) # Get the CTR data from Google Analytics. ctr_data = googleanalytics.search_ctr(start, end) # Create the metrics. clicks_kind = MetricKind.objects.get_or_create( code=SEARCH_CLICKS_METRIC_CODE)[0] searches_kind = MetricKind.objects.get_or_create( code=SEARCH_SEARCHES_METRIC_CODE)[0] for date_str, ctr in list(ctr_data.items()): day = datetime.strptime(date_str, "%Y-%m-%d").date() # Note: we've been storing our search data as total number of # searches and clicks. Google Analytics only gives us the rate, # so I am normalizing to 1000 searches (multiplying the % by 10). # I didn't switch everything to a rate because I don't want to # throw away the historic data. Metric.objects.create(kind=searches_kind, start=day, end=day + timedelta(days=1), value=1000) Metric.objects.create( kind=clicks_kind, start=day, end=day + timedelta(days=1), value=round(ctr, 1) * 10, )
def update_search_ctr_metric(): """Get new search CTR data from Google Analytics and save.""" if settings.STAGE: # Let's be nice to GA and skip on stage. return # Start updating the day after the last updated. latest_metric = _get_latest_metric(SEARCH_CLICKS_METRIC_CODE) if latest_metric is not None: latest_metric_date = latest_metric.start else: latest_metric_date = date(2011, 01, 01) start = latest_metric_date + timedelta(days=1) # Collect up until yesterday end = date.today() - timedelta(days=1) # Get the CTR data from Google Analytics. ctr_data = googleanalytics.search_ctr(start, end) # Create the metrics. clicks_kind = MetricKind.objects.get(code=SEARCH_CLICKS_METRIC_CODE) searches_kind = MetricKind.objects.get(code=SEARCH_SEARCHES_METRIC_CODE) for date_str, ctr in ctr_data.items(): day = datetime.strptime(date_str, '%Y-%m-%d').date() # Note: we've been storing our search data as total number of # searches and clicks. Google Analytics only gives us the rate, # so I am normalizing to 1000 searches (multiplying the % by 10). # I didn't switch everything to a rate because I don't want to # throw away the historic data. Metric.objects.create( kind=searches_kind, start=day, end=day + timedelta(days=1), value=1000) Metric.objects.create( kind=clicks_kind, start=day, end=day + timedelta(days=1), value=round(ctr, 1) * 10)
def update_search_ctr_metric(): """Get new search CTR data from Google Analytics and save.""" # Start updating the day after the last updated. latest_metric = _get_latest_metric(SEARCH_CLICKS_METRIC_CODE) if latest_metric is not None: latest_metric_date = latest_metric.start else: latest_metric_date = date(2011, 01, 01) start = latest_metric_date + timedelta(days=1) # Collect up until yesterday end = date.today() - timedelta(days=1) # Get the CTR data from Google Analytics. ctr_data = googleanalytics.search_ctr(start, end) # Create the metrics. clicks_kind = MetricKind.objects.get(code=SEARCH_CLICKS_METRIC_CODE) searches_kind = MetricKind.objects.get(code=SEARCH_SEARCHES_METRIC_CODE) for date_str, ctr in ctr_data.items(): day = datetime.strptime(date_str, '%Y-%m-%d').date() # Note: we've been storing our search data as total number of # searches and clicks. Google Analytics only gives us the rate, # so I am normalizing to 1000 searches (multiplying the % by 10). # I didn't switch everything to a rate because I don't want to # throw away the historic data. Metric.objects.create( kind=searches_kind, start=day, end=day + timedelta(days=1), value=1000) Metric.objects.create( kind=clicks_kind, start=day, end=day + timedelta(days=1), value=round(ctr, 1) * 10)