// 貼り付け用コード
function trackEvent(eventType, eventProperties, cb) {
const sid = "プロファイルidを入力してください";
// プロジェクトIDを入れてください!
const area = "jp";
const options = eventProperties || {};
loadPtengineScript(sid, area)
.then(() => {
console.log("ptengineスクリプトの読み込み完了");
window.ptengine && window.ptengine.track(eventType, options);
cb && typeof cb === "function" && cb();
})
.catch((error) => {
console.error("ptengineスクリプトの読み込み失敗:", error);
});
}
function loadPtengineScript(sid, area) {
return new Promise((resolve, reject) => {
if (window.ptengine) {
resolve();
} else {
const url = window.location.href.replace(
/\/wpm@[a-zA-Z0-9]+\/web-pixel-[a-zA-Z0-9]+@[a-zA-Z0-9]+\/sandbox\/modern/,
""
);
window._pt_sp_2 = [];
_pt_sp_2.push(`setAccount, ${sid}`);
_pt_sp_2.push(`setPVTag,${url},replace`);
const script = document.createElement("script");
const isCheckoutPage = window.location.href.includes("checkouts");
const sandboxQuery = isCheckoutPage ? "" : "?sandbox";
script.src = `https://js.ptengine.${area}/${sid}.js${sandboxQuery}`;
script.onload = () => resolve();
script.onerror = () => reject(new Error("Script loading failed"));
document.head.appendChild(script);
}
});
}
analytics.subscribe("checkout_completed", (event) => {
const checkout = event?.data?.checkout;
const checkoutTotalPrice = checkout?.totalPrice?.amount;
const checkoutItems = checkout?.lineItems;
const checkoutOrderId = checkout?.order?.id;
const uid = event?.data?.clientId;
const eventProperties = {
totalPrice: checkoutTotalPrice,
totalorderid: checkoutOrderId,
};
const identifyCallback = () => {
checkoutItems.forEach((checkoutItem) => {
ptengine &&
ptengine.identify(uid, {
totalPrice: checkoutTotalPrice,
totalorderid: checkoutOrderId,
totalquantity: checkoutItem?.quantity,
totaltitle: checkoutItem?.title,
totalid: checkoutItem?.id,
totalsku: checkoutItem?.variant?.sku,
totalProductType: checkoutItem?.variant?.product?.type,
});
});
};
trackEvent("checkout_completed", eventProperties, identifyCallback);
});
analytics.subscribe("search_submitted", (event) => {
trackEvent("search_submitted");
});
analytics.subscribe("collection_viewed", (event) => {
trackEvent("collection_viewed");
});
analytics.subscribe("product_viewed", (event) => {
trackEvent("product_viewed");
});
analytics.subscribe("product_added_to_cart", (event) => {
trackEvent("product_added_to_cart");
});
analytics.subscribe("product_removed_from_cart", (event) => {
trackEvent("product_removed_from_cart");
});
analytics.subscribe("cart_viewed", (event) => {
trackEvent("cart_viewed");
});
analytics.subscribe("checkout_started", (event) => {
trackEvent("checkout_started");
});
analytics.subscribe("checkout_contact_info_submitted", (event) => {
trackEvent("checkout_contact_info_submitted");
});
analytics.subscribe("checkout_address_info_submitted", (event) => {
trackEvent("checkout_address_info_submitted");
});
analytics.subscribe("checkout_shipping_info_submitted", (event) => {
trackEvent("checkout_shipping_info_submitted");
});
analytics.subscribe("payment_info_submitted", (event) => {
trackEvent("payment_info_submitted");
});