| 1 |
import $ from 'jquery'; |
| 2 |
import { |
| 3 |
getOrCreateBackgroundApp, |
| 4 |
initBackgroundApp, |
| 5 |
} from '../utils/backgroundAppUtils'; |
| 6 |
import { domElements } from '../constants/selectors'; |
| 7 |
import { connectionStatus, activationTime } from '../constants/leadinConfig'; |
| 8 |
import { fetchAccessToken } from '../api/wordpressApiClient'; |
| 9 |
import { ProxyMessages } from '../iframe/integratedMessages'; |
| 10 |
|
| 11 |
const REVIEW_BANNER_INTRO_PERIOD_DAYS = 15; |
| 12 |
|
| 13 |
const userIsAfterIntroductoryPeriod = () => { |
| 14 |
const activationDate = new Date(+activationTime * 1000); |
| 15 |
const currentDate = new Date(); |
| 16 |
const timeElapsed = new Date( |
| 17 |
currentDate.getTime() - activationDate.getTime() |
| 18 |
); |
| 19 |
|
| 20 |
return timeElapsed.getUTCDate() - 1 >= REVIEW_BANNER_INTRO_PERIOD_DAYS; |
| 21 |
}; |
| 22 |
|
| 23 |
/** |
| 24 |
* Adds some methods to window when review banner is |
| 25 |
* displayed to monitor events |
| 26 |
*/ |
| 27 |
export function initMonitorReviewBanner() { |
| 28 |
if (connectionStatus !== 'Connected') return; |
| 29 |
|
| 30 |
fetchAccessToken() |
| 31 |
.then( |
| 32 |
({ |
| 33 |
accessToken, |
| 34 |
expiresIn, |
| 35 |
}: { |
| 36 |
accessToken: string; |
| 37 |
expiresIn: number; |
| 38 |
}) => { |
| 39 |
const embedder = getOrCreateBackgroundApp(accessToken, expiresIn); |
| 40 |
const container = $(domElements.reviewBannerContainer); |
| 41 |
if (container && userIsAfterIntroductoryPeriod()) { |
| 42 |
$(domElements.reviewBannerLeaveReviewLink) |
| 43 |
.off('click') |
| 44 |
.on('click', () => { |
| 45 |
embedder.postMessage({ |
| 46 |
key: ProxyMessages.TrackReviewBannerInteraction, |
| 47 |
}); |
| 48 |
}); |
| 49 |
|
| 50 |
$(domElements.reviewBannerDismissButton) |
| 51 |
.off('click') |
| 52 |
.on('click', () => { |
| 53 |
embedder.postMessage({ |
| 54 |
key: ProxyMessages.TrackReviewBannerDismissed, |
| 55 |
}); |
| 56 |
}); |
| 57 |
|
| 58 |
embedder |
| 59 |
.postAsyncMessage({ |
| 60 |
key: ProxyMessages.FetchContactsCreateSinceActivation, |
| 61 |
payload: +activationTime * 1000, |
| 62 |
}) |
| 63 |
.then(({ total }: any) => { |
| 64 |
if (total >= 5) { |
| 65 |
container.removeClass('leadin-review-banner--hide'); |
| 66 |
embedder.postMessage({ |
| 67 |
key: ProxyMessages.TrackReviewBannerRender, |
| 68 |
}); |
| 69 |
} |
| 70 |
}); |
| 71 |
} |
| 72 |
} |
| 73 |
) |
| 74 |
.catch(err => |
| 75 |
console.error('[leadin] Failed to load review banner embedder:', err) |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
initBackgroundApp(initMonitorReviewBanner); |
| 80 |
|