@cronjobs.register
def update_search_ctr_metric():
 if settings.STAGE:
  return
 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, 1, 1)
 start = (latest_metric_date + timedelta(days=1))
 end = (date.today() - timedelta(days=1))
 ctr_data = googleanalytics.search_ctr(start, end)
 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()
  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))

@ cronjobs . register def update_search_ctr_metric ( ) : if settings . STAGE : return 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 , 1 , 1 ) start = ( latest_metric_date + timedelta ( days = 1 ) ) end = ( date . today ( ) - timedelta ( days = 1 ) ) ctr_data = googleanalytics . search_ctr ( start , end ) 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 ( ) 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 ) )
get new search ctr data from google analytics and save .