From 0695d96500eb54662d96c34a0ed9aba49a942566 Mon Sep 17 00:00:00 2001 From: Brian Lovin Date: Tue, 22 May 2018 14:52:26 -0700 Subject: [PATCH] Ensure user exists in search tracking events --- api/queries/search/searchCommunities.js | 18 ++++++++++-------- api/queries/search/searchCommunityMembers.js | 18 ++++++++++-------- api/queries/search/searchThreads.js | 20 +++++++++++--------- api/queries/search/searchUsers.js | 18 ++++++++++-------- 4 files changed, 41 insertions(+), 33 deletions(-) diff --git a/api/queries/search/searchCommunities.js b/api/queries/search/searchCommunities.js index 6b52db268e..04537dd2c2 100644 --- a/api/queries/search/searchCommunities.js +++ b/api/queries/search/searchCommunities.js @@ -12,14 +12,16 @@ export default (args: Args, { loaders, user }: GraphQLContext) => { return communitySearchIndex .search({ query: queryString }) .then(content => { - trackQueue.add({ - userId: user.id, - event: events.SEARCHED_COMMUNITIES, - properties: { - queryString, - hitsCount: content.hits ? content.hits.length : 0, - }, - }); + if (user && user.id) { + trackQueue.add({ + userId: user.id, + event: events.SEARCHED_COMMUNITIES, + properties: { + queryString, + hitsCount: content.hits ? content.hits.length : 0, + }, + }); + } if (!content.hits || content.hits.length === 0) return []; const communityIds = content.hits.map(o => o.objectID); diff --git a/api/queries/search/searchCommunityMembers.js b/api/queries/search/searchCommunityMembers.js index 3e0b1ed7ee..a74fe8ec67 100644 --- a/api/queries/search/searchCommunityMembers.js +++ b/api/queries/search/searchCommunityMembers.js @@ -16,14 +16,16 @@ export default (args: Args, { loaders, user }: GraphQLContext) => { return usersSearchIndex .search({ query: queryString, hitsPerPage }) .then(content => { - trackQueue.add({ - userId: user.id, - event: events.SEARCHED_COMMUNITY_MEMBERS, - properties: { - queryString, - hitsCount: content.hits ? content.hits.length : 0, - }, - }); + if (user && user.id) { + trackQueue.add({ + userId: user.id, + event: events.SEARCHED_COMMUNITY_MEMBERS, + properties: { + queryString, + hitsCount: content.hits ? content.hits.length : 0, + }, + }); + } if (!content.hits || content.hits.length === 0) return []; // if no search filter was passed, there's no way to be searching for diff --git a/api/queries/search/searchThreads.js b/api/queries/search/searchThreads.js index 333f752235..b61c1db4c3 100644 --- a/api/queries/search/searchThreads.js +++ b/api/queries/search/searchThreads.js @@ -38,15 +38,17 @@ export default async (args: Args, { loaders, user }: GraphQLContext) => { threadsSearchIndex .search({ query: queryString, filters }) .then(content => { - trackQueue.add({ - userId: user.id, - event: events.SEARCHED_CONVERSATIONS, - properties: { - queryString, - filters, - hitsCount: content.hits ? content.hits.length : 0, - }, - }); + if (user && user.id) { + trackQueue.add({ + userId: user.id, + event: events.SEARCHED_CONVERSATIONS, + properties: { + queryString, + filters, + hitsCount: content.hits ? content.hits.length : 0, + }, + }); + } if (!content.hits || content.hits.length === 0) return null; return content.hits.map(o => ({ diff --git a/api/queries/search/searchUsers.js b/api/queries/search/searchUsers.js index a4857f7511..7a4b89fe30 100644 --- a/api/queries/search/searchUsers.js +++ b/api/queries/search/searchUsers.js @@ -21,14 +21,16 @@ export default (args: Args, { loaders, user }: GraphQLContext) => { ? events.SEARCHED_COMMUNITY_MEMBERS : events.SEARCHED_USERS; - trackQueue.add({ - userId: user.id, - event, - properties: { - queryString, - hitsCount: content.hits ? content.hits.length : 0, - }, - }); + if (user && user.id) { + trackQueue.add({ + userId: user.id, + event, + properties: { + queryString, + hitsCount: content.hits ? content.hits.length : 0, + }, + }); + } if (!content.hits || content.hits.length === 0) return [];