def update_visitors_metric(): """Get new visitor data from Google Analytics and save.""" # Start updating the day after the last updated. latest_metric = _get_latest_metric(VISITORS_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 visitor data from Google Analytics. visitors = googleanalytics.visitors(start, end) # Create the metrics. metric_kind = MetricKind.objects.get(code=VISITORS_METRIC_CODE) for date_str, visits in visitors.items(): day = datetime.strptime(date_str, '%Y-%m-%d').date() Metric.objects.create( kind=metric_kind, start=day, end=day + timedelta(days=1), value=visits)
def test_visitors(self, _build_request): """Test googleanalytics.visitors().""" execute = _build_request.return_value.get.return_value.execute execute.return_value = VISITORS_RESPONSE visits = googleanalytics.visitors( date(2013, 01, 16), date(2013, 01, 16)) eq_(1, len(visits)) eq_(382719, visits['2013-01-16'])
def update_visitors_metric(): """Get new visitor data from Google Analytics and save.""" # Start updating the day after the last updated. latest_metric = _get_latest_metric(VISITORS_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 visitor data from Google Analytics. visitors = googleanalytics.visitors(start, end) # Create the metrics. metric_kind = MetricKind.objects.get(code=VISITORS_METRIC_CODE) for date_str, visits in visitors.items(): day = datetime.strptime(date_str, '%Y-%m-%d').date() Metric.objects.create(kind=metric_kind, start=day, end=day + timedelta(days=1), value=visits)