Files

41 lines
1.3 KiB
TypeScript

import { storage } from './storage';
import * as StoreReview from 'expo-store-review';
const ACTIONS_COUNT_KEY = 'user_actions_count';
const LAST_PROMPT_DATE_KEY = 'last_review_prompt_date';
export const ActionTracker = {
/**
* Increment action count and check if we should show review prompt
*/
async trackAction(minActions: number = 10, enabled: boolean = true) {
if (!enabled) return;
try {
const currentCountStr = await storage.get(ACTIONS_COUNT_KEY);
const currentCount = parseInt(currentCountStr || '0', 10) + 1;
await storage.save(ACTIONS_COUNT_KEY, currentCount.toString());
if (currentCount >= minActions) {
const lastPrompt = await storage.get(LAST_PROMPT_DATE_KEY);
const now = new Date().getTime();
// Only prompt once every 30 days
const thirtyDays = 30 * 24 * 60 * 60 * 1000;
if (!lastPrompt || (now - parseInt(lastPrompt, 10)) > thirtyDays) {
if (await StoreReview.isAvailableAsync()) {
await StoreReview.requestReview();
await storage.save(LAST_PROMPT_DATE_KEY, now.toString());
// Reset counter after successful prompt
await storage.save(ACTIONS_COUNT_KEY, '0');
}
}
}
} catch (error) {
console.warn('[ActionTracker] Error:', error);
}
}
};