2022-12-10 00:43:09 +00:00
|
|
|
import { URLPattern } from 'urlpattern-polyfill';
|
|
|
|
|
|
|
|
export const isPatternMatchingWithURL = (urlPattern, url) => {
|
|
|
|
let updatedUrlPattern = urlPattern;
|
|
|
|
const locationObj = new URL(url);
|
|
|
|
|
|
|
|
if (updatedUrlPattern.endsWith('/')) {
|
|
|
|
updatedUrlPattern = updatedUrlPattern.slice(0, -1) + '*\\?*\\#*';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (locationObj.pathname.endsWith('/')) {
|
|
|
|
locationObj.pathname = locationObj.pathname.slice(0, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const pattern = new URLPattern(updatedUrlPattern);
|
|
|
|
return pattern.test(locationObj.toString());
|
2021-05-17 16:08:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Format all campaigns
|
2021-05-18 06:45:23 +00:00
|
|
|
export const formatCampaigns = ({ campaigns }) => {
|
|
|
|
return campaigns.map(item => {
|
2021-05-17 16:08:35 +00:00
|
|
|
return {
|
|
|
|
id: item.id,
|
2021-10-12 12:28:33 +00:00
|
|
|
triggerOnlyDuringBusinessHours:
|
|
|
|
item.trigger_only_during_business_hours || false,
|
2021-05-17 16:08:35 +00:00
|
|
|
timeOnPage: item?.trigger_rules?.time_on_page,
|
|
|
|
url: item?.trigger_rules?.url,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-10-12 12:28:33 +00:00
|
|
|
// Filter all campaigns based on current URL and business availability time
|
|
|
|
export const filterCampaigns = ({
|
|
|
|
campaigns,
|
|
|
|
currentURL,
|
|
|
|
isInBusinessHours,
|
|
|
|
}) => {
|
2021-11-22 18:02:17 +00:00
|
|
|
return campaigns.filter(campaign => {
|
2022-12-10 00:43:09 +00:00
|
|
|
if (!isPatternMatchingWithURL(campaign.url, currentURL)) {
|
2021-11-22 18:02:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (campaign.triggerOnlyDuringBusinessHours) {
|
|
|
|
return isInBusinessHours;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2021-05-17 16:08:35 +00:00
|
|
|
};
|