File: /var/www/gurumedia_st_usr/data/www/gurumedia.store/frd-public/theme/asset/js/frd-capi.js
/*
* FRD CAPI JS - Version 23 (FINAL + 100% META-OPTIMIZED)
* Only: PageView, ViewContent, AddToCart, InitiateCheckout
* Purchase sent from PHP (checkout-complete.php)
* Author: Grok (xAI) + SpiderEcommerce
*/
if (frplug_capii == 1) {
document.addEventListener("DOMContentLoaded", function () {
// === CAPI ইভেন্ট পাঠানো ===
function sendCapiEvent(eventName, extraData = {}) {
const payload = {
event_name: eventName,
event_id: FRc_EVENT_IDD,
external_id: FRc_EXTERNAL_IDD || '',
event_source_url: FRc_THIS_PAGE_URLL || window.location.href,
page_title: document.title || 'Unknown'
};
// === extraData মার্জ ===
Object.assign(payload, extraData);
// === content_ids → সবসময় অ্যারে + strval() ===
if (payload.content_ids !== undefined) {
let ids = payload.content_ids;
if (typeof ids === 'string') {
try { ids = JSON.parse(ids); }
catch { ids = ids.split(',').map(s => s.trim()).filter(Boolean); }
}
payload.content_ids = Array.isArray(ids)
? ids.map(id => String(id)).filter(Boolean)
: [];
} else {
payload.content_ids = [];
}
// === value → ন্যূনতম 0.01 (Meta রিজেক্ট করে 0) ===
if (payload.value !== undefined) {
payload.value = Math.max(parseFloat(payload.value) || 0, 0.01);
}
// === FormData তৈরি (URLSearchParams) ===
const formData = new URLSearchParams();
for (const [key, value] of Object.entries(payload)) {
if (value === null || value === undefined || value === '') continue;
formData.append(key, Array.isArray(value) ? JSON.stringify(value) : String(value));
}
// === CAPI পাঠানো ===
fetch(`${FRD_HURLL}/frdsp/dp/page/frd-p-fbcapi/frdapi-IniCAPI.php`, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: formData,
keepalive: true // ← পেজ আনলোড হলেও পাঠাবে
}).catch(err => {
console.error("CAPI Send Error:", err);
});
}
// === PageView ===
sendCapiEvent("PageView");
// === ViewContent ===
if (FR_PG_RR === "product" && FRc_CT_ItemIdd) {
sendCapiEvent("ViewContent", {
value: Math.max(parseFloat(FRc_CT_ItemSealPricee) || 0, 0.01),
currency: "BDT",
content_type: "product",
content_ids: FRc_CT_ItemIdd,
content_name: FRc_CT_ItemNamee || '',
content_category: FRc_CT_ItemCatNamee || ''
});
}
// === AddToCart (AJAX থেকে কল হবে) ===
window.FrFun_CAPI_AddToCartEventFire = function(data) {
let item = typeof data === 'string' ? JSON.parse(data) : data;
if (!item?.item_id || !item?.price) return;
sendCapiEvent("AddToCart", {
value: Math.max(parseFloat(item.price) || 0, 0.01),
currency: "BDT",
content_type: "product",
content_ids: [String(item.item_id)],
content_name: item.item_name || '',
content_category: item.item_category || '',
num_items: parseInt(item.quantity) || 1
});
};
// === InitiateCheckout ===
if (FR_PG_RR === "checkout" && FRc_CT_ItemIdd) {
sendCapiEvent("InitiateCheckout", {
value: Math.max(parseFloat(FRc_CT_ItemSealPricee) || 0, 0.01),
currency: "BDT",
content_type: "product",
content_ids: FRc_CT_ItemIdd,
content_name: FRc_CT_ItemNamee || '',
content_category: FRc_CT_ItemCatNamee || '',
num_items: parseInt(FRc_CT_CartItems_CC) || 1
});
}
// === লগ (ডিবাগ) ===
console.log("%cCAPI JS v23 | Active | Event ID: " + FRc_EVENT_IDD, "color: #00bfff; font-weight: bold;");
});
}