chore: Execute campaigns based on matching URL (#2254)
This commit is contained in:
@@ -1,14 +1,25 @@
|
||||
import { triggerCampaign } from 'widget/api/campaign';
|
||||
const startTimer = async ({ allCampaigns }) => {
|
||||
allCampaigns.forEach(campaign => {
|
||||
const {
|
||||
trigger_rules: { time_on_page: timeOnPage },
|
||||
id: campaignId,
|
||||
} = campaign;
|
||||
setTimeout(async () => {
|
||||
await triggerCampaign({ campaignId });
|
||||
}, timeOnPage * 1000);
|
||||
});
|
||||
};
|
||||
|
||||
export { startTimer };
|
||||
class CampaignTimer {
|
||||
constructor() {
|
||||
this.campaignTimers = [];
|
||||
}
|
||||
|
||||
initTimers = ({ campagins }) => {
|
||||
this.clearTimers();
|
||||
campagins.forEach(campaign => {
|
||||
const { timeOnPage, id: campaignId } = campaign;
|
||||
this.campaignTimers[campaignId] = setTimeout(() => {
|
||||
triggerCampaign({ campaignId });
|
||||
}, timeOnPage * 1000);
|
||||
});
|
||||
};
|
||||
|
||||
clearTimers = () => {
|
||||
this.campaignTimers.forEach(timerId => {
|
||||
clearTimeout(timerId);
|
||||
this.campaignTimers[timerId] = null;
|
||||
});
|
||||
};
|
||||
}
|
||||
export default new CampaignTimer();
|
||||
|
||||
Reference in New Issue
Block a user