Skip to content

Instantly share code, notes, and snippets.

@Keith-Hon
Last active August 27, 2024 06:25
Show Gist options
  • Save Keith-Hon/9042e6bb67789c8bf79fbf47fd830f25 to your computer and use it in GitHub Desktop.
Save Keith-Hon/9042e6bb67789c8bf79fbf47fd830f25 to your computer and use it in GitHub Desktop.
retention
import { NextApiRequest, NextApiResponse } from "next";
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const periods = [1, 7, 14, 30];
const output = {};
for (let j = 0; j < periods.length; j++) {
const period = periods[j];
const threshold = new Date(new Date().getTime() - 60 * 24 * 60 * 60 * 1000).toISOString();
// const threshold = new Date(new Date().getTime() - 365 * 24 * 60 * 60 * 1000).toISOString(); // consider all numbers
const usersResponse = await fetch(process.env.STRAPI_URL + '/api/mobile-numbers?filters[createdAt][$gt]=' + threshold + '&populate[0]=whatsapp_messages&populate[1]=whatsapp_audio_messages&populate[2]=whatsapp_vision_messages&populate[3]=whatsapp_youtube_summary_messages&populate[4]=whatsapp_image_generation_messages&pagination[limit]=100000', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.STRAPI_TOKEN}`
}
})
const usersData = await usersResponse.json();
let totalCount = 0;
let userCount = 0;
console.log(`Total users: ${usersData.data.length}`);
for (let i = 0; i < usersData.data.length; i++) {
const user = usersData.data[i];
const userCreatedAt = new Date(user.attributes.createdAt);
if (!user.attributes.number.startsWith('852')) {
continue;
}
const thresholdFrom = new Date(userCreatedAt.getTime() + period * 24 * 60 * 60 * 1000);
const thresholdTo = new Date(userCreatedAt.getTime() + (period + 1) * 24 * 60 * 60 * 1000);
let isUserActive = false;
// check if user has any activity in the 7th day after the create date
const whatsappMessagesCount = user.attributes.whatsapp_messages.data.filter((message) => (new Date(message.attributes.createdAt) > new Date(thresholdFrom)) && (new Date(message.attributes.createdAt) < new Date(thresholdTo))).length;
if (whatsappMessagesCount > 0) {
isUserActive = true;
}
const whatsappAudioMessagesCount = user.attributes.whatsapp_audio_messages.data.filter((message) => (new Date(message.attributes.createdAt) > new Date(thresholdFrom)) && (new Date(message.attributes.createdAt) < new Date(thresholdTo))).length;
if (whatsappAudioMessagesCount > 0) {
isUserActive = true;
}
const whatsappVisionMessagesCount = user.attributes.whatsapp_vision_messages.data.filter((message) => (new Date(message.attributes.createdAt) > new Date(thresholdFrom)) && (new Date(message.attributes.createdAt) < new Date(thresholdTo))).length;
if (whatsappVisionMessagesCount > 0) {
isUserActive = true;
}
const whatsappYoutubeSummaryMessagesCount = user.attributes.whatsapp_youtube_summary_messages.data.filter((message) => (new Date(message.attributes.createdAt) > new Date(thresholdFrom)) && (new Date(message.attributes.createdAt) < new Date(thresholdTo))).length;
if (whatsappYoutubeSummaryMessagesCount > 0) {
isUserActive = true;
}
const whatsappImageGenerationMessagesCount = user.attributes.whatsapp_image_generation_messages.data.filter((message) => (new Date(message.attributes.createdAt) > new Date(thresholdFrom)) && (new Date(message.attributes.createdAt) < new Date(thresholdTo))).length;
if (whatsappImageGenerationMessagesCount > 0) {
isUserActive = true;
}
if (isUserActive) {
userCount += 1;
}
totalCount += 1;
}
output['retention_' + period + '_days'] = userCount / totalCount;
}
return res.status(200).json({ ...output });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment