I'm trying to receive the email statistics for multiple categories at once from Sendgrid API. I have @sendgrid/client installed and I'm using this peace of code:
const client = require('@sendgrid/client')
client.setDefaultRequest('qsStringifyOptions', {arrayFormat: 'repeat'});
client.setApiKey(process.env.SENDGRID_API_KEY)
const request = {}
const queryParams = {
'aggregated_by': 'day',
'categories': ['cat1', 'cat2'],
'start_date': '2021-08-01',
'end_date': '2021-09-01',
'limit': 5,
'offset': 1
};
request.qs = queryParams;
request.method = 'GET';
request.url = '/v3/categories/stats';
client
.request(request)
.then(([response, body]) => {
console.log(response.statusCode);
console.log(response.body);
}).catch(function (error) {
console.log(error)
})
taken from https://github.com/sendgrid/sendgrid-nodejs/blob/main/packages/client/USAGE.md#get-categoriesstats. When I execute it I receive Error Code 400 and "body: { errors: [Array] }". When I replace 'categories': ['cat1', 'cat2'], with 'categories': cat1', it works. It returns Code 200 and valid Object with stats in it. What am I missing? Is this some kind of syntax error. I don't have much experience with API's.
Appreciate all your help.
After couple chat sessions with Sendgrid support here what is end situation. By using following code
const client = require('@sendgrid/client')
client.setApiKey(process.env.SENDGRID_API_KEY)
client.setDefaultRequest('qsStringifyOptions', {arrayFormat: 'repeat'});
const request = {}
const queryParams = {
'aggregated_by': 'day',
'categories': 'dallas',
'start_date': '2021-08-31',
'end_date': '2021-09-01',
'limit': 1,
'offset': 1
};
request.method = 'GET';
request.url = '/v3/categories/stats?start_date=2021-09-01&categories=cat1&categories=cat2&limit=99&aggregated_by=week';
client
.request(request)
.then(([response]) => {
console.log(response.statusCode);
console.log(response.body);
}).catch(function (error) {
console.error(error)
})
I can retrieve stats for cat1 and cat2, but if you looking for campaigns containing both categories cat1 AND cat2, you can't do that with API. Let say you have a multiple people working on multiple markets and you are looking for stats for specific person on specific market, best solution is when creating email blast to add multiple categories: 1) salesperson_name 2) market 3) market&salesperson_name and then pull stats for market&salesperson_name category.