Search code examples
facebookfacebook-graph-apifacebook-marketing-api

Facebook Ads Insights: How to get data aggregated by Sun - Sat week, instead of Mon - Sun week?


In ads insights api, when I try to call an end point with time_increment set to 7, it groups the data by week.

However the week that it uses is a Monday-Sunday week. How can I get the data grouped by a Sunday - Saturday week?

An additional info is that we are a reporting solution provider , and we fetch data for different clients that are spread across the globe. Some clients want the data grouped by Monday-Sunday week, where as some want the data to be grouped by Sunday - Saturday week. So I was wondering is this something that is configurable.


Solution

  • The semantics of that API parameters imply that Facebook API itself doesn't force any notion of a week.

    So you need to set TimeRange parameter in your request in such way that first day of your parameter is Sunday:

    request: {
        time_range: {
            since: getClosestSundayInThePast(startDate),
            until: getClosestSundayInTheFuture(endDate).minus(1, 'day')
        },
        time_increment: 7
    }
    

    Implementation of getClosestSunday(Date) I'll leave to you, it shouldn't be hard.

    Alternatively, you can prebuild an array of TimeRange objects (with syntax similar to above):

    var date = startDate;
    var ranges = [date];
    do {
      date = date.plus(7, 'day');
      ranges.push(date)
    } while (date < endDate);
    
    request: {
        time_ranges: ranges
    }
    

    For more details (such as date formats, etc.), see also an API documentation.