Fill in Missing Dates

When running a daily report in the AdSense Host API, you may expect to get a row for each day, regardless of what data is being returned. However, in some cases you may get no data back for a given day, simply because there were no logged events of the requested types.

Dimension: DATE
Metric: CLICKS, EARNINGS

Response:

{
 "kind": "adsense#report",
 "totalMatchedRows": "4",
 "headers": [
  { "name": "DATE", "type": "DIMENSION" },
  { "name": "CLICKS", "type": "METRIC_TALLY" }
  { "name": "EARNINGS", "type": "METRIC_CURRENCY" }
 ],
 "rows": [
  [ "2014-01-08", "3", "0.41" ],
  [ "2014-01-09", "5", "0.49" ],
  [ "2014-01-12", "2", "0.19" ],
  [ "2014-01-13", "1", "0.03" ]
 ],
 "totals": [ "", "13", "1.12" ],
 "averages": [ "", "2", "0.28" ],
 "startDate": "2014-01-08",
 "endDate": "2014-01-13"
}

As you can see, there are no rows being returned for 2014-01-10 or 2014-01-11, since there were no click or earnings events.

Adding missing data

To add the missing data, you’ll need to iterate through all the dates between the start and end dates and see if data is present for every field. If not, you’ll need to replace every metric with the appropriate default value (zero in most cases).

Here are some samples in different programming languages:

Next steps