const xEventMap = { AddToCart: 'tw-pn6ci-pn6cm', ViewPricePlan: 'tw-pn6ci-przrx', Purchase: 'tw-pn6ci-pn6cj', Answered5CorrectQuestions: 'tw-pn6ci-przsx', Answered20CorrectQuestions: 'tw-pn6ci-przsy', Answered50CorrectQuestions: 'tw-pn6ci-przsz', StartTrial: 'tw-pn6ci-przso', NoG1: 'tw-pn6ci-prztp' }; /* === 生成唯一 event_id === */ function generateEventId() { return 'ev-' + Date.now() + '-' + Math.random().toString(36).substr(2, 9); } /* === 轻量封装:像 sendCapiEvent 一样,同时返回 event_id === */ function capiAndPixelTrack(eventName, pixelPayload = {}, capiPayload = {}) { const eid = generateEventId(); // ① 生成 // ② Pixel 侧:带上 eventID fbq('track', eventName, pixelPayload, { eventID: eid }); // for x if (typeof twq === 'function') { // ① 根据 fb eventName 找到 X 的事件标识 const xEventId = xEventMap[eventName] || eventName; // ② 用 'event' 方法触发,并带上 conversion_id twq('event', xEventId, { ...pixelPayload, conversion_id: eid }); } // ③ CAPI 侧:带上 event_id sendCapiEvent(eventName, { ...capiPayload, event_id: eid }); } // 从 Cookie 里提取完整的 fb.1.xxx fbc function getFbcFromCookie() { const match = document.cookie.match(/(?:^|; )_fbc=(fb\.1\.\d+\.[^;]+)/); return match ? decodeURIComponent(match[1]) : null; } function sendCapiEvent(eventName, options = {}) { const email = options.email || localStorage.getItem('userEmail'); // 可选 const body = { event_name: eventName, event_source_url: options.event_source_url || window.location.href }; if (options.event_id) body.event_id = options.event_id; if (email) { body.email = email; } // ⇒ 从 Cookie 里拿完整的 fbc const fbc = getFbcFromCookie(); if (fbc) { body.fbc = fbc; } // ⇒ 同理可加 fbp(浏览器 ID) const fbpMatch = document.cookie.match(/(?:^|; )_fbp=(fb\.1\.\d+\.[^;]+)/); if (fbpMatch) { body.fbp = decodeURIComponent(fbpMatch[1]); } if (options.value) { body.value = options.value; } if (options.currency) { body.currency = options.currency; } fetch('capi-track.php', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }).then(res => { if (!res.ok) console.warn('CAPI event failed:', eventName); }); }