diff --git a/app/analytics/__tests__/query.test.ts b/app/analytics/__tests__/query.test.ts index 43fd4ea..0b4e1db 100644 --- a/app/analytics/__tests__/query.test.ts +++ b/app/analytics/__tests__/query.test.ts @@ -416,6 +416,30 @@ describe("AnalyticsEngineAPI", () => { }, }); }); + + test("should handle case where there are no results", async () => { + fetch + .mockResolvedValueOnce( + createFetchResponse({ + data: [], // no visitor count results + }), + ) + .mockResolvedValueOnce( + createFetchResponse({ + data: [], // no non-visitor results + }), + ); + + const result = await api.getAllCountsByColumn( + "example.com", + "path", + "DAY", + "America/New_York", + ); + + expect(result).toEqual({}); + expect(fetch).toHaveBeenCalledTimes(2); + }); }); describe("getEarliestEvents", () => { diff --git a/app/analytics/query.ts b/app/analytics/query.ts index 11f8e55..9e11983 100644 --- a/app/analytics/query.ts +++ b/app/analytics/query.ts @@ -486,9 +486,11 @@ export class AnalyticsEngineAPI { let filterStr = filtersToSql(filters); const _column = ColumnMappings[column]; + if (keys.length > 0) { - filterStr += `AND ${_column} IN (${keys.map((key) => `'${key}'`).join(", ")})`; + filterStr += ` AND ${_column} IN (${keys.map((key) => `'${key}'`).join(", ")})`; } + const query = ` SELECT ${_column}, ${ColumnMappings.newVisitor} as isVisitor,