(function () {
const trustooEnv = "api.trustoo.io";
// 当前页面是否为感谢页
if (Shopify.Checkout) {
// 是否存在uv和已pv的产品
const clientId = localStorage.getItem("trustoo_uv");
const pvProId = sessionStorage.getItem("tt_pv_product_id");
const checkoutToken = Shopify.Checkout.token;
if (clientId !== "null" && pvProId !== "null") {
let data = "";
if (Shopify.checkout && Shopify.checkout.order_id) {
data += `&order_id=${Shopify.checkout.order_id}`;
}
if (checkoutToken) {
data += `&checkout_token=${checkoutToken}`;
}
// 判断购物车中有没有已pv的产品
// if (Shopify.checkout.line_items.some(it => it.product_id == pvProId)) {
// 符合条件,发送记录
if (data) {
fetch(
location.origin +
`/apps/trustoo-proxy/api/v1/reviews/collect?client_id=${clientId}&event_category=checkout${data}`
);
sessionStorage.removeItem("tt_pv_product_id");
}
// }
}
const order_id = (Shopify.checkout && Shopify.checkout.order_id) || 0;
let settings = null,
words = null;
let wordsLoadedCallback = null;
let isWordsLoaded = false;
const shop_id =
window.ShopifyAnalytics &&
ShopifyAnalytics.lib &&
ShopifyAnalytics.lib.config &&
ShopifyAnalytics.lib.config.Trekkie.defaultAttributes.shopId;
const shop = (window.Shopify && window.Shopify.shop) || "";
const lang = (Shopify && Shopify.locale) || "en";
const style = document.createElement("style");
style.textContent = `
.tt-referral-term:hover {text-decoration: underline;}
@keyframes tt_show_window {0% {transform: scale(0.6);}100% {transform: scale(1);}
`;
document.head.appendChild(style);
let url = `https://${trustooEnv}/api/v1/reviews/get_referral_setting?`;
// let url =
let getShareLinkUrl = `https://${trustooEnv}/api/v1/reviews/get_share_link?`;
let getAutoReviewSettings = `https://${trustooEnv}/api/v1/reviews/get_auto_leave_review_setting?scene=thank_you&`;
let flag = false;
let shareLink = "";
const params = { shop, shop_id, order_id, checkout_token: checkoutToken };
const paramsStr = Object.keys(params)
.reduce(
(pre, it) => (params[it] ? `${pre}&${it}=${params[it]}` : pre),
""
)
.slice(1);
// if (shop_id) {
// flag = true;
// url += `shop_id=${shop_id}&`;
// }
// if (order_id) {
// url += `&order_id=${order_id}`;
// getShareLinkUrl += `&order_id=${order_id}`;
// getAutoReviewSettings += `&order_id=${order_id}`;
// }
// if (shop) {
// flag = true;
// url += `shop=${shop}`;
// }
if (!paramsStr) {
return;
}
url += paramsStr;
getShareLinkUrl += paramsStr;
getAutoReviewSettings += paramsStr;
// const requests = [
// fetch(url),
// fetch(
// `https://${trustooEnv}/api/v1/reviews/get_customer_self_define_lang?shop_id=${shop_id}&lang=${lang}`
// ),
// ];
fetch(url)
.then(response => response.json())
.then(async data => {
let settingsData = data;
if (settingsData.code !== 0) {
throw { ...settingsData };
}
settings = settingsData.data;
if (settings.is_active !== 1) {
return;
}
isWordsLoaded = true;
const wordsData = await fetch(
`https://${trustooEnv}/api/v1/reviews/get_customer_self_define_lang?shop_id=${shop_id}&lang=${lang}`
).then(response => response.json());
console.log(wordsData);
if (wordsData.code !== 0) {
throw { ...wordsData };
}
const allWords = JSON.parse(wordsData.data.lang_json);
if (wordsLoadedCallback) {
wordsLoadedCallback(allWords.thankyou);
}
words = allWords.referral;
words.discount = words.discount.replace(
"{{friend_discount}}",
settings.friend_discount_amount
);
words.reward = words.reward.replace(
"{{advocate_reward}}",
settings.advocate_reward
);
words.reward_desc = words.reward_desc
.replace("{{friend_discount}}", settings.friend_discount_amount)
.replace("{{advocate_reward}}", settings.advocate_reward);
insertReferral();
})
.catch(error => {
console.log("Request for Trustoo referral failed:", error);
});
function insertReferral(params) {
const contentNode = document.querySelector(".content-box");
contentNode.insertAdjacentHTML(
"afterend",
`
${
words.discount
}
${
words.reward
}
${words.gift}
${words.reward_desc}
${words.code}
${settings.new_customers_only === 1 ? `
${words.newOnly}
` : ""}
${
settings.minimum_purchase_amount
? `
${words.amount_tip.replace(
"{{minimum_amount}}",
settings.minimum_purchase_amount
)}
`
: ""
}
Terms of Service
${settings.shop_name} Referral Program – Terms of Service
You may earn rewards by successfully referring customers to the
store, by sharing your unique link.
A successful referral must meet the following criteria: 1. An order
made
directly through your unique link. If your link was used by a
referred customer who leaves the page and purchases later, this will
disable the referral tracking.
2. If a minimum purchase amount is specified, the order must meet
the required purchase amount or higher.
3. The order made by the referred customer was successfully
fulfilled.
It is prohibited to refer yourself, including using a different
email address, and to distribute your referral link via commercial
advertising or spam.
Once a successful referral has been detected, an email containing
information on how to claim your reward will be sent out to you. It
may take up to a day after the referral purchase is fulfilled for
the referral to be detected.
Rewards are accumulated separately for each unique link. If you make
more than one purchase at the store, each purchase will result in
generating a different link.
Any abuse of this offer may result in the rescission of your
referral credit and the referred customer's promo code as well as
both parties' inability to participate in this or future promotions.
Referral credit cannot be applied to previous purchases, and is not
redeemable for cash. This referral program is subject to
modification or termination at any time without notice.
`
);
const card = document.getElementById("tt-referral-card");
const wrapper = document.getElementById("tt-referral-wrapper");
const btn = card.querySelector(".tt-referral-button");
const popup = card.querySelector(".tt-referral-popup");
const shareA = popup.querySelectorAll(".tt-referral-popup a");
const term = wrapper.querySelector(".tt-referral-term");
const mask = document.getElementById("trustoo-mask");
const termWrapper = mask.querySelector("#term-wrapper");
const close = mask.querySelector(".tt-mask-closer");
const copyBtn = popup.querySelector(".tt-referral-copy");
const link = popup.querySelector(".tt-referral-link");
copyBtn.onclick = () => {
var range = document.createRange(); //创建一个range
window.getSelection().removeAllRanges();
range.selectNode(link); // 选中需要复制的节点
window.getSelection().addRange(range); // 执行选中元素
var successful = document.execCommand("copy"); // 执行 copy 操作
if (successful) {
setToast(words.copied);
}
// 移除选中的元素
window.getSelection().removeAllRanges();
};
window.addEventListener("click", e => {
if (!popup.contains(e.target)) {
popup.style.display = "none";
}
});
btn.onclick = e => {
e.stopPropagation();
const isFlex = popup.style.display == "flex";
popup.style.display = isFlex ? "none" : "flex";
if (!shareLink) {
fetch(getShareLinkUrl)
.then(response => response.json())
.then(data => {
if (data.code !== 0) {
throw data;
}
shareLink = data.data.share_link;
link.textContent = shareLink;
shareA.forEach(it => (it.href += shareLink));
})
.catch(err => console.log(err));
}
};
term.onclick = e => {
e.stopPropagation();
mask.style.display = "flex";
document.body.style.overflow = "hidden";
};
close.onclick = e => {
e.stopPropagation();
mask.style.display = "none";
document.body.style.overflow = "auto";
};
mask.onclick = e => {
if (!termWrapper.contains(e.target)) {
mask.style.display = "none";
document.body.style.overflow = "auto";
}
};
function setToast(msg, top = "10%", left = "50%") {
var node = document.createElement("div");
node.style.cssText = `left:${left};top:${top};box-sizing: border-box;position: fixed;padding: 9px 16px;border-radius: 8px;color: #fff;font-size: 16px;line-height: 1.4;background-color: #151515;transition: all 0.1s;transform: translateX(-50%);text-align: center;z-index: 9999999999;`;
node.className = "toast";
node.innerHTML = msg;
document.body.appendChild(node);
setTimeout(() => {
document.querySelector(".toast").remove();
}, 2000);
}
}
fetch(getAutoReviewSettings)
.then(res => res.json())
.then(res => {
if (res.code !== 0) {
throw { ...res };
}
const settings = res.data;
if (settings.is_active !== 1) {
return;
}
insertAutoReviews(settings);
const mask = document.getElementById("tt-auto-mask");
const wrapper = document.getElementById("tt-auto-reviews");
const title = wrapper.querySelector("#tt-auto-title");
const desc = wrapper.querySelector("#tt-auto-desc");
const primary = wrapper.querySelector("#tt-auto-primary");
const secondary = wrapper.querySelector("#tt-auto-secondary");
wordsLoadedCallback = words => {
console.log(words);
title.textContent = words.title;
desc.textContent = words.desc.replace(
"{{Store Name}}",
settings.shop_name
);
primary.textContent = words.agree;
secondary.textContent = words.notAgree;
mask.style.display = "flex";
};
if (isWordsLoaded) {
} else {
fetch(
`https://${trustooEnv}/api/v1/reviews/get_customer_self_define_lang?shop_id=${shop_id}&lang=${lang}`
)
.then(response => response.json())
.then(wordsData => {
if (wordsData.code !== 0) {
throw { ...wordsData };
}
words = JSON.parse(wordsData.data.lang_json);
wordsLoadedCallback(words.thankyou);
});
}
})
.catch(err => console.log(err));
function insertAutoReviews(settings) {
const autoReviews = `
About automatic positive reviews
By agreeing, you authorize Store Name to automatically submit positive
reviews on your behalf if no reviews are made within a certain period
after you place an order, and you can update these reviews at any time
`;
document.body.insertAdjacentHTML("beforeend", autoReviews);
const primary = document.getElementById("tt-auto-primary");
const secondary = document.getElementById("tt-auto-secondary");
primary.onclick = () => setAutoLeaveReview(true);
secondary.onclick = () => setAutoLeaveReview(false);
}
function setAutoLeaveReview(val) {
fetch(`https://${trustooEnv}/api/v1/reviews/set_auto_leave_review`, {
method: "POST", // 使用POST方法
headers: {
"Content-Type": "application/json", // 设置内容类型为JSON
},
body: JSON.stringify({
shop,
order_id,
is_agree: val ? 1 : 0,
checkout_token: checkoutToken,
scene: "thank_you",
}), // 将对象转换为JSON字符串
});
document.getElementById("tt-auto-mask").remove();
}
}
})();