Event Analytics pagination cursor "after" does not work
drea-wav opened this issue · comments
Referring to: GET /events/v3/events/
A successful call to hubspotClient.events.eventsApi.getPage() returns the "paging" object where you find the "after" cursor (paging.next.after). For instance, "
However, if you're trying to paginate through your events, you'll find that any "after" cursors returned do not work, meaning that you're only able to query the first page of events.
I've tried this with several different objects. Every time I try to paginate, the first page contains 20 events, and the next returns 0. I've even tried to insert the cursor as a string directly into my query, resulting in nothing being returned.
Additionally, it appears that the Hubspot client is attempting to interpret the cursor as an ISO date, even though it's in this format: "MTcxMjY5MDQ1MDM2MnwwLTF8NzA4MDM5NjM0NXw2LTI3NTgyNDU5fC03NTgwNjg4Mjk%3D"
Pagination Code
async function getHubspotEvents(eventType, after = undefined) {
let allEvents = [];
try {
while (true) {
console.log("after", after);
const apiResponse = await hubspotClient.events.eventsApi.getPage(
undefined,
eventType,
after
);
console.log("apiResponse.results.length", apiResponse.results.length);
allEvents.push(...apiResponse.results);
console.log("apiResponse.paging", apiResponse?.paging?.next);
if (apiResponse?.paging?.next) {
after = apiResponse?.paging?.next?.after;
console.log("set after", after);
} else {
return;
}
}
} catch (e) {
e.message === "HTTP request failed"
? console.error(JSON.stringify(e.response, null, 2))
: console.error(e);
}
return allEvents;
}
(async () => {
const atcs = await getHubspotEvents("pe8778399_add_to_cart_v3");
console.log(atcs.length);
})();
Putting the cursor directly into the query
async function getHubspotEvents(eventType, after = undefined) {
let allEvents = [];
try {
while (true) {
console.log("after", after);
const apiResponse = await hubspotClient.events.eventsApi.getPage(
undefined,
eventType,
(after =
"MTcxMjY5MDQ1MDM2MnwwLTF8NzA4MDM5NjM0NXw2LTI3NTgyNDU5fC03NTgwNjg4Mjk%3D")
);
console.log("apiResponse.results.length", apiResponse.results.length);
allEvents.push(...apiResponse.results);
console.log("apiResponse.paging", apiResponse?.paging?.next);
if (apiResponse?.paging?.next) {
after = apiResponse?.paging?.next?.after;
console.log("set after", after);
} else {
return;
}
}
} catch (e) {
e.message === "HTTP request failed"
? console.error(JSON.stringify(e.response, null, 2))
: console.error(e);
}
return allEvents;
}
(async () => {
const atcs = await getHubspotEvents("pe8778399_add_to_cart_v3");
console.log(atcs.length);
})();
ISO Date Error
TypeError: data.toISOString is not a function
at ObjectSerializer.serialize (/Users/andreadielman/node_modules/@hubspot/api-client/lib/codegen/events/models/ObjectSerializer.js:143:29)
at EventsApiRequestFactory.<anonymous> (/Users/andreadielman/node_modules/@hubspot/api-client/lib/codegen/events/apis/EventsApi.js:33:99)
at Generator.next (<anonymous>)
at /Users/andreadielman/node_modules/@hubspot/api-client/lib/codegen/events/apis/EventsApi.js:8:71
at new Promise (<anonymous>)
at __awaiter (/Users/andreadielman/node_modules/@hubspot/api-client/lib/codegen/events/apis/EventsApi.js:4:12)
at EventsApiRequestFactory.getPage (/Users/andreadielman/node_modules/@hubspot/api-client/lib/codegen/events/apis/EventsApi.js:21:16)
at ObservableEventsApi.getPageWithHttpInfo (/Users/andreadielman/node_modules/@hubspot/api-client/lib/codegen/events/types/ObservableAPI.js:14:59)
at ObservableEventsApi.getPage (/Users/andreadielman/node_modules/@hubspot/api-client/lib/codegen/events/types/ObservableAPI.js:29:21)
at PromiseEventsApi.getPage (/Users/andreadielman/node_modules/@hubspot/api-client/lib/codegen/events/types/PromiseAPI.js:14:33)
Hi @andreadielman
The problem is putting after
to occurredAfter?: Date
(the third param).
after
is the eighth parameter in getPage
method.
Workable example:
async function main() {
let allEvents = [];
let after;
try {
while (true) {
console.log("after", after);
const apiResponse = await hubspotClient.events.eventsApi.getPage(
undefined,
eventType,
undefined,
undefined,
undefined,
undefined,
undefined,
after
);
console.log("apiResponse.results.length", apiResponse.results.length);
allEvents.push(...apiResponse.results);
console.log("apiResponse.paging", apiResponse?.paging?.next);
if (apiResponse?.paging?.next) {
after = apiResponse?.paging?.next?.after;
console.log("set after", after);
} else {
return;
}
}
} catch (e) {
e.message === "HTTP request failed"
? console.error(JSON.stringify(e.response, null, 2))
: console.error(e);
}
console.log(allEvents);
}
Version of the SDK - 11.1.0