From 56eb7a822f1ad7815cf62df7a98216eb186b5291 Mon Sep 17 00:00:00 2001 From: Rishabh Date: Tue, 4 Apr 2023 23:46:05 +0530 Subject: [PATCH] Fixed FirstPromoter integration to ignore old referrals closes https://github.com/TryGhost/Team/issues/2888 Due to lack of member's created at date, we previously called the FirstPromoter tracking function for all logged-in members irrespective of when they signed up. This caused issues in few cases where members who were already signed up were getting falsely attributed as referrals if they had clicked on a referral link previously. This change uses the member's created at date which is now available on frontend for logged-in members, and ignored the FirstPromoter tracking script if member had signed up more than 24 hours ago. --- ghost/core/core/server/services/members/utils.js | 1 + ghost/portal/src/App.js | 4 ++-- ghost/portal/src/utils/helpers.js | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/ghost/core/core/server/services/members/utils.js b/ghost/core/core/server/services/members/utils.js index eee5eb68b3..80ecf3329e 100644 --- a/ghost/core/core/server/services/members/utils.js +++ b/ghost/core/core/server/services/members/utils.js @@ -20,6 +20,7 @@ module.exports.formattedMemberResponse = function formattedMemberResponse(member subscribed: !!member.subscribed, subscriptions: member.subscriptions || [], paid: member.status !== 'free', + created_at: member.created_at, enable_comment_notifications: member.enable_comment_notifications }; if (member.newsletters) { diff --git a/ghost/portal/src/App.js b/ghost/portal/src/App.js index 17cd01b4e5..e4f41f3dab 100644 --- a/ghost/portal/src/App.js +++ b/ghost/portal/src/App.js @@ -11,7 +11,7 @@ import {getActivePage, isAccountPage, isOfferPage} from './pages'; import ActionHandler from './actions'; import './App.css'; import NotificationParser from './utils/notifications'; -import {allowCompMemberUpgrade, createPopupNotification, getCurrencySymbol, getFirstpromoterId, getPriceIdFromPageQuery, getProductCadenceFromPrice, getProductFromId, getQueryPrice, getSiteDomain, isActiveOffer, isComplimentaryMember, isInviteOnlySite, isPaidMember, isSentryEventAllowed, removePortalLinkFromUrl} from './utils/helpers'; +import {allowCompMemberUpgrade, createPopupNotification, getCurrencySymbol, getFirstpromoterId, getPriceIdFromPageQuery, getProductCadenceFromPrice, getProductFromId, getQueryPrice, getSiteDomain, isActiveOffer, isComplimentaryMember, isInviteOnlySite, isPaidMember, isRecentMember, isSentryEventAllowed, removePortalLinkFromUrl} from './utils/helpers'; import {handleDataAttributes} from './data-attributes'; import i18nLib from '@tryghost/i18n'; @@ -558,7 +558,7 @@ export default class App extends React.Component { if (!_t || 'complete' === _t || 'loaded' === _t) { try { window.$FPROM.init(firstPromoterId, siteDomain); - if (member) { + if (isRecentMember({member})) { const email = member.email; const uid = member.uuid; if (window.$FPROM) { diff --git a/ghost/portal/src/utils/helpers.js b/ghost/portal/src/utils/helpers.js index d052acd3a4..f032f0144d 100644 --- a/ghost/portal/src/utils/helpers.js +++ b/ghost/portal/src/utils/helpers.js @@ -891,3 +891,17 @@ export function getUrlHistory() { console.warn(`[Portal] Failed to load member URL history:`, error); } } + +// Check if member is a recent member, i.e. created in last 24 hours +export function isRecentMember({member}) { + if (!member?.created_at) { + return false; + } + + const now = new Date(); + const created = new Date(member.created_at); + const diff = now.getTime() - created.getTime(); + const diffHours = Math.round(diff / (1000 * 60 * 60)); + + return diffHours < 24; +}