Untitled

 avatar
unknown
javascript
10 months ago
2.0 kB
12
Indexable
getEngagementTag(): string | null {
		const sentMessages = this.data.sent || [];
		const openedMessages = this.data.opened || [];
		const clickedMessages = this.data.clicked || [];

		if (sentMessages.length === 0) {
			return null;
		}

		const sortedSent = [...sentMessages].sort(
			(a, b) => new Date(b.date).getTime() - new Date(a.date).getTime(),
		);

		if (sortedSent.length >= 3) {
			const lastThreeMessages = sortedSent.slice(0, 3);
			const mostRecentMessageDate = new Date(lastThreeMessages[0].date);
			const now = new Date();
			const oneWeekAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);

			if (mostRecentMessageDate <= oneWeekAgo) {
				const hasOpenedAnyOfLastThree = lastThreeMessages.some((sent) =>
					openedMessages.some((opened) => opened.id === sent.id),
				);

				if (!hasOpenedAnyOfLastThree) {
					return "Retarget";
				}
			}
		}

		if (sortedSent.length >= 5) {
			const lastTenMessages = sortedSent.slice(0, 10);

			const opensInLastTen = lastTenMessages.filter((sent) =>
				openedMessages.some((opened) => opened.id === sent.id),
			).length;

			const openRate = opensInLastTen / lastTenMessages.length;

			if (openRate >= 0.75) {
				const hasClickedInLastTen = lastTenMessages.some((sent) =>
					clickedMessages.some((clicked) => clicked.id === sent.id),
				);

				if (hasClickedInLastTen) {
					return "Highly Engaged";
				}
				return "Engaged";
			}
		}

		return null;
	}

	getSpendingTag(fansSortedByLTVDesc: Fan[]): string | null {
		if (!this.data.ltv || this.data.ltv <= 0) {
			return null;
		}

		if (fansSortedByLTVDesc.length === 0) return null;

		const fanIndex = fansSortedByLTVDesc.findIndex((f) => f.id === this.id);
		if (fanIndex === -1) return "Purchased";

		const percentile = (fanIndex / fansSortedByLTVDesc.length) * 100;

		if (percentile < 1) return "Top 1% spender";
		if (percentile < 10) return "Top 10% spender";
		if (percentile < 50) return "Top 50% spender";

		return "Purchased";
	}
Editor is loading...
Leave a Comment