如何在Gumroad监听器中添加代码实现购买后跳转自定义URL
How to Add Custom Post-Purchase Redirect to Gumroad Overlay Listener
Got it, let's tackle this! Your existing code already correctly listens for Gumroad's sale postMessage event—we just need to add the redirect logic right inside that event handler. Here's how to modify it properly:
Step 1: Update the Listener Function
Replace your current listener with this robust version that includes the redirect (plus error handling to avoid unexpected crashes):
const gumroadPurchaseListener = function(ev) { try { // Safely parse the incoming message data const messageData = JSON.parse(ev.data); // Check if this is a completed sale event if (messageData.post_message_name === "sale") { // Replace this with your actual thank-you page URL const customThankYouUrl = "https://your-thank-you-page.com"; // Option 1: Regular redirect (users can click back to return to the purchase page) window.location.href = customThankYouUrl; // Option 2: Replace page in history (prevents back button from returning to purchase flow) // window.location.replace(customThankYouUrl); } } catch (error) { // Log errors for debugging if needed console.error("Error processing Gumroad purchase message:", error); } }; window.addEventListener('message', gumroadPurchaseListener, false);
Step 2: Optional: Pass Purchase Data to Your Thank-You Page
If you want to carry order details (like order ID, product name, or buyer email) over to your thank-you page, you can append them as URL parameters:
// Inside the "sale" condition block: const purchase = messageData.purchase; const product = messageData.product; // Build URL parameters with key purchase details const urlParams = new URLSearchParams({ orderId: purchase.id, productName: product.name, buyerEmail: purchase.email }); // Redirect with parameters included window.location.href = `https://your-thank-you-page.com?${urlParams.toString()}`;
Quick Tips:
- Always use a fully qualified URL (include
https://orhttp://) for your redirect target. - The
try/catchblock ensures your code won't break if Gumroad sends an unexpected message format. - Choose
window.location.replace()overhrefif you don't want users to navigate back to the purchase page via the browser's back button.
内容的提问来源于stack exchange,提问作者kurblivas
相关产品推荐
相关产品推荐

