Skip to content

Commit

Permalink
fix issue with totalResultsCount
Browse files Browse the repository at this point in the history
  • Loading branch information
avermeil committed Mar 21, 2024
1 parent 18bba8c commit e8a3074
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ export class Customer extends ServiceFactory {

private async paginatedSearch(
gaqlQuery: string,
requestOptions: Readonly<RequestOptions>
requestOptions: Readonly<
RequestOptions & { return_total_results_count?: boolean } // We do not allow return_total_results_count in reportOptions, however it is still a valid request option
>
): Promise<{
response: services.IGoogleAdsRow[];
totalResultsCount?: number;
Expand All @@ -249,7 +251,6 @@ export class Customer extends ServiceFactory {
*/
if (
requestOptions.page_size === undefined &&
// @ts-expect-error we do not allow this field in reportOptions, however it is still a valid request option
requestOptions.return_total_results_count === undefined
) {
// If no pagination or summary options are set, we can use the non-paginated search method.
Expand All @@ -264,7 +265,17 @@ export class Customer extends ServiceFactory {
const response: services.IGoogleAdsRow[] = [];
let nextPageToken: PageToken = undefined;
const initialSearch = await this.search(gaqlQuery, requestOptions);
const totalResultsCount = initialSearch.totalResultsCount;

let totalResultsCount = initialSearch.totalResultsCount;
// Sometimes (when no results?) the totalResultsCount field is not included in the response.
// In this case, we set it to 0.
if (
requestOptions.return_total_results_count &&
initialSearch.totalResultsCount === undefined
) {
totalResultsCount = 0;
}

let summaryRow = initialSearch.summaryRow;

response.push(...initialSearch.response);
Expand Down

0 comments on commit e8a3074

Please sign in to comment.