如何在网站上同时部署多个TikTok Pixel
Got it, adding a second TikTok Pixel to your site is totally doable—TikTok's Analytics SDK supports multiple pixel instances natively, so you won’t have to overhaul your existing setup. Here’s how to make it work step by step:
1. Update the Initialization Script
Modify your existing TikTok Pixel code to load the second pixel ID and create a dedicated instance for it. Your original pixel setup stays intact; we just add the new one alongside it:
<script> !function (w, d, t) { w.TiktokAnalyticsObject=t;var ttq=w[t]=w[t]||[];ttq.methods=["page","track","identify","instances","debug","on","off","once","ready","alias","group","enableCookie","disableCookie"],ttq.setAndDefer=function(t,e){t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}};for(var i=0;i<ttq.methods.length;i++)ttq.setAndDefer(ttq,ttq.methods[i]);ttq.instance=function(t){for(var e=ttq._i[t]||[],n=0;n<ttq.methods.length;n++ )ttq.setAndDefer(e,ttq.methods[n]);return e},ttq.load=function(e,n){var i="https://analytics.tiktok.com/i18n/pixel/events.js";ttq._i=ttq._i||{},ttq._i[e]=[],ttq._i[e]._u=i,ttq._t=ttq._t||{},ttq._t[e]=+new Date,ttq._o=ttq._o||{},ttq._o[e]=n||{};n=document.createElement("script");n.type="text/javascript",n.async=!0,n.src=i+"?sdkid="+e+"&lib="+t;e=document.getElementsByTagName("script")[0];e.parentNode.insertBefore(n,e)}; // Load your original pixel (keep this as-is) ttq.load('MY_PIXEL_ID'); ttq.page(); // Fire page view for the first pixel // Load the second pixel ttq.load('SECOND_PIXEL_ID'); // Create an instance to interact with the second pixel const secondTTQ = ttq.instance('SECOND_PIXEL_ID'); secondTTQ.page(); // Fire page view for the second pixel }(window, document, 'ttq'); </script>
2. Trigger Events for Both Pixels
You can fire events separately for each pixel, or create a helper to send events to both at once—whichever fits your workflow:
Option A: Trigger Events Separately
Use the original ttq object for your first pixel, and the secondTTQ instance for the new one:
// Fire AddToCart for your original pixel (your existing code stays the same) ttq.track('AddToCart',{ content_id: productId, quantity: 1, price: productPrice, value: productPrice, currency: 'PKR' }); // Fire the same (or modified) event for the second pixel secondTTQ.track('AddToCart',{ content_id: productId, quantity: 1, price: productPrice, value: productPrice, currency: 'PKR' });
Option B: Trigger Events on Both Pixels at Once
To avoid duplicating code, create a helper function to send events to all your pixels:
function trackAllPixels(eventName, eventData) { // Send to first pixel ttq.track(eventName, eventData); // Send to second pixel secondTTQ.track(eventName, eventData); } // Use this helper whenever you need to track an event trackAllPixels('AddToCart',{ content_id: productId, quantity: 1, price: productPrice, value: productPrice, currency: 'PKR' });
Key Notes
- Each pixel instance works independently, so you can customize event data for each if needed (e.g., different currency values or content IDs).
- Don’t forget to call
page()for both pixels during initialization to ensure page views are tracked correctly for each. - You can add more than two pixels following the same pattern: load the ID, create an instance, and fire the page view.
内容的提问来源于stack exchange,提问作者Shoaib Munir

