=== All-in-one CAPI for Meta & Pinterest + GTM === Contributors: suhanduman Tags: capi, conversion api, meta pixel, pinterest, GTM Requires at least: 6.0 Tested up to: 6.9 Stable tag: 3.3.0 Requires PHP: 7.4 WC requires at least: 8.0 WC tested up to: 10.7 License: GPLv2 or later License URI: https://www.gnu.org/licenses/gpl-2.0.html Free server-side CAPI for Meta & Pinterest + GTM dataLayer. Cache-safe, theme-agnostic, no cloud server needed. == Description == Server-side tracking for your WooCommerce store — without a GTM Server Container, without a premium plugin, without a monthly bill. This plugin sends Meta (Facebook) and Pinterest Conversions API events from your WordPress server, and sets up a clean GTM dataLayer so your browser-side tags (Pixel, GA4, Pinterest Tag) work alongside it. Same event ID on both sides, so Meta merges them into one event instead of double-counting. **What it does:** * **Meta & Pinterest CAPI** — PageView, ViewContent, AddToCart, InitiateCheckout, Purchase, Search, AddToWishlist, Lead, and the full checkout funnel (AddShippingInfo, AddPaymentInfo). Sent server-side with hashed user data for high Event Match Quality. * **GTM dataLayer** — Automatic script injection and a standards-compliant dataLayer for GA4, Meta Pixel, Pinterest Tag, or anything else you run through GTM. * **Full WooCommerce coverage** — 14 event types, classic checkout and block checkout, HPOS compatible. * **Cache-safe** — Works with LiteSpeed Cache, WP Rocket, Varnish, and Cloudflare full-page cache. Events carry a unique random ID per visit so Meta never deduplicates cached traffic into one event. * **Batch delivery** — Events are queued and sent in bulk every 60 seconds. No synchronous HTTP on user requests, no latency added to page loads. * **Built-in debug log** — See exactly which events were sent, to which platform, and whether the API accepted them. Filterable by event type, with success/failure stats. * **REST + AJAX fallback** — Browser events go to a REST endpoint (no nonce, cache-proof) and fall back to admin-ajax.php if REST is blocked by a firewall. **What you don't need:** * No GTM Server Container (saves $30-150/month in cloud fees) * No premium tier — every feature is in the free plugin * No external tracking SaaS subscription **Please check the installation page for the recommended GTM settings for deduplication.** == Our Philosophy == This plugin is free. Not "free with limits" — just free. Every feature works, no pro version behind a paywall. I built it because setting up server-side tracking shouldn't require a cloud engineering degree or a monthly bill. If it helps your store, that's good enough. == Frequently Asked Questions == = Does this plugin replace the Meta Pixel? = No, it works alongside it. The plugin sends server-side (CAPI) events, while GTM handles the browser-side Pixel. Both use the same `event_id`, so Meta merges them automatically without counting anything twice. = What is the difference between this and a GTM Server Container? = A GTM Server Container runs on Google Cloud and costs money every month. This plugin does the same job directly from your WordPress server — no extra infrastructure, no extra bill. = Does it work with page caching plugins (WP Rocket, LiteSpeed, etc.)? = Yes. PageView and ViewCategory events fire from JavaScript, so they work even on fully cached pages. Cart, checkout, and purchase pages are not cached by default. = What plugins are required? = WooCommerce. That's it. If you use other GTM plugins (like Google Site Kit), disable their e-commerce features to avoid conflicts. = Is there a pro version? = No. Everything is included. = My events aren't showing in Meta Events Manager. What's wrong? = Check these in order: 1. Open your **Event Log** tab in the plugin settings. If events appear there with "Success (Meta)", the plugin is sending them. If Meta isn't receiving them, the problem is on Meta's end — usually Pixel ID or Access Token mismatch. 2. If the log is empty after visiting your store, you likely have a JS optimizer (LiteSpeed / WP Rocket / Autoptimize) deferring the plugin's tracking scripts. See next answer. 3. Check the admin notice on the plugin settings page — the plugin auto-detects your cache setup and shows exclude-list instructions. = I use LiteSpeed Cache. How do I configure it? = Go to **LiteSpeed Cache → Page Optimization → JS Settings → JS Defer Excludes** and add these script IDs (one per line): `mcapi-pageview-init` `mcapi-viewcontent-events` `mcapi-viewcategory-events` `mcapi-frontend-events` Then purge all cache. = I use WP Rocket. How do I configure it? = Go to **WP Rocket → File Optimization → JavaScript → Excluded JavaScript Files** and add the same script IDs listed above for LiteSpeed. Then clear WP Rocket cache. = I use Autoptimize. How do I configure it? = Go to **Autoptimize → JS, CSS & HTML → Exclude scripts from Autoptimize** and add the same script IDs. = What about Cloudflare Rocket Loader? = The plugin adds `data-cfasync="false"` to its inline scripts, which Cloudflare Rocket Loader respects. No configuration needed. = Does it work with a block-based theme (e.g. Twenty Twenty-Five)? = Yes. The plugin's tracking works with the WooCommerce Products block used by FSE themes. == External Services == This plugin connects your website to external services to send event data. * **Service Used:** Meta Conversion API * **Purpose:** To send user interaction and e-commerce event data from your server to Meta's servers for ad performance measurement, optimization, and audience building. * **Data Sent:** Event details (product ID, price) and user parameters (IP address, user agent, hashed email/name/phone, Facebook cookies) are sent when a user performs a key action. * **Service Used:** Pinterest Conversions API * **Purpose:** Same as the Meta CAPI, providing reliable tracking for ad performance and audience building on Pinterest. * **Data Sent:** Event details and hashed user parameters are sent upon user action. * **Service Used:** Google Tag Manager * **Purpose:** To load a JavaScript container from Google's servers that allows you to manage and deploy marketing and analytics tags. * **Data Sent:** The plugin provides your GTM Container ID to Google to fetch the correct script. GTM itself may collect data based on how you configure your tags. == Installation == 1. Upload the plugin folder to the `/wp-content/plugins/` directory and activate it. 2. Navigate to the **Meta CAPI & GTM** page from your main WordPress menu. 3. Enter your **GTM Container ID** to inject the GTM script on your site. 4. Enter your **Meta Pixel ID** and **API Access Token** for the server-side connection. Do the same for Pinterest if applicable. 5. Go to the "Event Management" tab to select which CAPI events you want to track. 6. Configure your GTM container using the instructions in the "Recommended GTM Setup" section below. --- ### Recommended GTM Setup **Crucial First Steps:** To prevent duplicate events and ensure data accuracy, you must configure two settings in your Meta account and GTM container. 1. **Turn Off Meta's Automatic Event Tracking:** * In your Meta Business Suite, navigate to **Events Manager** and select your Pixel. * Go to the **Settings** tab. * Scroll to the **Event Setup** section and turn **Off** the toggle for **Track Events Automatically Without Code**. This plugin will handle all event sending. 2. **Pause Automatically Created GTM Tags:** * In your GTM container, please pause or delete any automatically created tags that start with `FB_`. Since we will be creating our own tags manually, only they should be active. --- ### TEMPLATE SETUP To save time and prevent errors, we have created a GTM container template. You can import this file to automatically create all the necessary variables, triggers, and tags. **Step 1: Download the Template** Go to the "Meta CAPI & GTM" settings page in your WordPress admin panel. In the Main Settings tab, you will see a highlighted box with a link to download the `gtm-template.json` template file. Download this file to your computer. **Step 2: Import the Template into GTM** 1. Go to your Google Tag Manager container. 2. Navigate to the **Admin** section. 3. Click on **Import Container**. 4. Click **Choose container file** and select the `gtm-template.json` file you downloaded. 5. Choose a **New** workspace and give it a descriptive name (e.g., "CAPI Import"). 6. **IMPORTANT:** Choose the **Merge** import option. Do NOT choose "Overwrite", as this could delete your existing tags. 7. The preview screen will show you all the new tags, triggers, and variables that will be added. Click **Confirm**. **Step 3: Configure Your IDs** After the import is complete, you must update the placeholder IDs with your own tracking IDs. 1. Go to the **Variables** section. 2. Find and click on the **"CONST - Meta Pixel ID"** variable. * Replace `META_PIXEL_ID` with your actual Meta Pixel ID. * Save the variable. All Meta tags will automatically use this value. 3. Go to the **Tags** section. 4. Find and click on the **"GA4 - Google Tag (Configuration)"** tag. * Replace `GA4_MEASUREMENT_ID` with your actual GA4 Measurement ID (e.g., `G-XXXXXXXXXX`). * Save the tag. 5. Find and click on the **"Pinterest - PageView"** tag. * Replace `PINTEREST_TAG_ID` with your actual Pinterest Tag ID. * Save the tag. **Step 4: Publish** Once you have updated your IDs, click the **Submit** button in the top right corner, then **Publish** your container. Your GTM setup is now complete! If you prefer to configure Google Tag Manager manually, or if you encounter any issues with the template import, the following guide provides step-by-step instructions to get everything configured. ### MANUAL SETUP ### Step 1: Create GTM Variables Create the following variables of the type **Data Layer Variable**. * **Variable Name:** `DLV - event_id` * **Data Layer Variable Name:** `event_id` * **Variable Name:** `DLV - ecommerce` * **Data Layer Variable Name:** `ecommerce` * **Variable Name:** `DLV - Hashed Email` * **Data Layer Variable Name:** `user_data.email` * **Variable Name:** `DLV - ecommerce.shipping_method` * **Data Layer Variable Name:** `ecommerce.shipping_method` * **Variable Name:** `DLV - ecommerce.payment_method` * **Data Layer Variable Name:** `ecommerce.payment_method` ### Step 2: Create GTM Triggers Create the following triggers using the **Custom Event** type. * **Trigger Name:** `CE - PageView Meta` * **Event name:** `page_view_meta` * **Trigger Name:** `CE - View Item` * **Event name:** `view_item` * **Trigger Name:** `CE - Add to Cart` * **Event name:** `add_to_cart` * **Trigger Name:** `CE - Begin Checkout` * **Event name:** `begin_checkout` * **Trigger Name:** `CE - Purchase` * **Event name:** `purchase` * **Trigger Name:** `CE - View Item List` * **Event name:** `view_item_list` * **Trigger Name:** `CE - View Cart` * **Event name:** `view_cart` * **Trigger Name:** `CE - Select Item` * **Event name:** `select_item` * **Trigger Name:** `CE - Add Shipping Info` * **Event name:** `add_shipping_info` * **Trigger Name:** `CE - Add Payment Info` * **Event name:** `add_payment_info` ### Step 3: Create GTM Tags #### A) Meta Tags (Facebook Pixel) Install the **"Facebook Pixel"** template from the GTM Community Template Gallery. Create the following tags. * **Tag: Meta - PageView** * **Event Name:** `PageView` * **More Settings > Event ID:** `{{DLV - event_id}}` * **Trigger:** `CE - PageView Meta` * **Tag: Meta - ViewContent** * **Event Name:** `ViewContent` * **Object Properties:** `{{DLV - ecommerce}}` * **More Settings > Event ID:** `{{DLV - event_id}}` * **Trigger:** `CE - View Item` * **Tag: Meta - AddToCart** * **Event Name:** `AddToCart` * **Object Properties:** `{{DLV - ecommerce}}` * **More Settings > Event ID:** `{{DLV - event_id}}` * **Trigger:** `CE - Add to Cart` * **Tag: Meta - InitiateCheckout** * **Event Name:** `InitiateCheckout` * **Object Properties:** `{{DLV - ecommerce}}` * **More Settings > Event ID:** `{{DLV - event_id}}` * **Trigger:** `CE - Begin Checkout` * **Tag: Meta - Purchase** * **Event Name:** `Purchase` * **Object Properties:** `{{DLV - ecommerce}}` * **More Settings > Event ID:** `{{DLV - event_id}}` * **Trigger:** `CE - Purchase` * **Tag: Meta - ViewCategory** * **Event Name:** `ViewCategory` * **Object Properties:** `{{DLV - ecommerce}}` * **More Settings > Event ID:** `{{DLV - event_id}}` * **Trigger:** `CE - View Item List` * **Tag: Meta - AddPaymentInfo** * **Event Name:** `AddPaymentInfo` * **Object Properties:** `{{DLV - ecommerce}}` * **More Settings > Event ID:** `{{DLV - event_id}}` * **Trigger:** `CE - Add Payment Info` #### B) Pinterest Tags **Prerequisite:** Install the **"Pinterest Tag"** template from the GTM Community Template Gallery. You must also create the following Data Layer Variables for e-commerce data: * `DLV - ecommerce.value` (Data Layer Variable Name: `ecommerce.value`) * `DLV - ecommerce.currency` (Data Layer Variable Name: `ecommerce.currency`) * `DLV - ecommerce.items` (Data Layer Variable Name: `ecommerce.items`) Create the following tags: * **Tag: Pinterest - PageView** * **Tag ID:** Your Pinterest Tag ID * **Hashed Email:** `{{DLV - Hashed Email}}` * **Event to Fire:** `page_visit` * **Custom Parameters:** Name `event_id`, Value `{{DLV - event_id}}` * **Trigger:** `CE - PageView Meta` * **Tag: Pinterest - ViewContent** * **Tag ID:** Your Pinterest Tag ID * **Hashed Email:** `{{DLV - Hashed Email}}` * **Event to Fire:** `pagevisit` * **Custom Parameters:** Add `event_id`, `value`, `currency`, and `contents` with their corresponding variables. * **Trigger:** `CE - View Item` * **Tag: Pinterest - AddToCart** * **Tag ID:** Your Pinterest Tag ID * **Hashed Email:** `{{DLV - Hashed Email}}` * **Event to Fire:** `add_to_cart` * **Custom Parameters:** Add `event_id`, `value`, `currency`, and `contents` with their corresponding variables. * **Trigger:** `CE - Add to Cart` * **Tag: Pinterest - InitiateCheckout** * **Tag ID:** Your Pinterest Tag ID * **Hashed Email:** `{{DLV - Hashed Email}}` * **Event to Fire:** `initiate_checkout` * **Custom Parameters:** Add `event_id`, `value`, `currency`, and `contents` with their corresponding variables. * **Trigger:** `CE - Begin Checkout` * **Tag: Pinterest - Purchase** * **Tag ID:** Your Pinterest Tag ID * **Hashed Email:** `{{DLV - Hashed Email}}` * **Event to Fire:** `checkout` * **Custom Parameters:** Add `event_id`, `value`, `currency`, and `contents` with their corresponding variables. * **Trigger:** `CE - Purchase` * **Tag: Pinterest - ViewCategory** * **Tag ID:** Your Pinterest Tag ID * **Hashed Email:** `{{DLV - Hashed Email}}` * **Event to Fire:** `viewcategory` * **Custom Parameters:** * Name `event_id`, Value `{{DLV - event_id}}` * Name `content_name`, Value `{{DLV - ecommerce.item_list_name}}` * **Trigger:** `CE - View Item List` #### C) Google Analytics 4 Tags **Prerequisite:** Before you begin, make sure you have your **Measurement ID** (starts with `G-`) from your Google Analytics 4 property. You must also create the following Data Layer Variables: * `DLV - ecommerce.currency` (Data Layer Variable Name: `ecommerce.currency`) * `DLV - ecommerce.value` (Data Layer Variable Name: `ecommerce.value`) * `DLV - ecommerce.items` (Data Layer Variable Name: `ecommerce.items`) * `DLV - ecommerce.transaction_id` (Data Layer Variable Name: `ecommerce.transaction_id`) * `DLV - ecommerce.item_list_name` (Data Layer Variable Name: `ecommerce.item_list_name`) First, create the main configuration tag that loads GA4 on all pages. * **Tag: GA4 - Google Tag (Configuration)** * **Tag Type:** Google Analytics > Google Tag * **Tag ID:** Your GA4 Measurement ID (e.g., `G-XXXXXXXXXX`) * **Important:** Uncheck the "Send a page view event when this configuration loads" box. We will send it manually with the next tag. * **Trigger:** `All Pages` Next, create the event tags that will send data from the `dataLayer` to Google Analytics. * **Tag: GA4 - Event - PageView** * **Tag Type:** Google Analytics > GA4 Event * **Configuration Tag:** Select your `GA4 - Google Tag (Configuration)` tag. * **Event Name:** `page_view` * **Trigger:** `CE - PageView Meta` * **Tag: GA4 - Event - ViewItem** * **Tag Type:** Google Analytics > GA4 Event * **Configuration Tag:** Select your `GA4 - Google Tag (Configuration)` tag. * **Event Name:** `view_item` * **Event Parameters:** * Parameter Name: `currency`, Value: `{{DLV - ecommerce.currency}}` * Parameter Name: `value`, Value: `{{DLV - ecommerce.value}}` * Parameter Name: `items`, Value: `{{DLV - ecommerce.items}}` * **Trigger:** `CE - View Item` * **Tag: GA4 - Event - AddToCart** * **Tag Type:** Google Analytics > GA4 Event * **Configuration Tag:** Select your `GA4 - Google Tag (Configuration)` tag. * **Event Name:** `add_to_cart` * **Event Parameters:** * Parameter Name: `currency`, Value: `{{DLV - ecommerce.currency}}` * Parameter Name: `value`, Value: `{{DLV - ecommerce.value}}` * Parameter Name: `items`, Value: `{{DLV - ecommerce.items}}` * **Trigger:** `CE - Add to Cart` * **Tag: GA4 - Event - BeginCheckout** * **Tag Type:** Google Analytics > GA4 Event * **Configuration Tag:** Select your `GA4 - Google Tag (Configuration)` tag. * **Event Name:** `begin_checkout` * **Event Parameters:** * Parameter Name: `currency`, Value: `{{DLV - ecommerce.currency}}` * Parameter Name: `value`, Value: `{{DLV - ecommerce.value}}` * Parameter Name: `items`, Value: `{{DLV - ecommerce.items}}` * **Trigger:** `CE - Begin Checkout` * **Tag: GA4 - Event - Purchase** * **Tag Type:** Google Analytics > GA4 Event * **Configuration Tag:** Select your `GA4 - Google Tag (Configuration)` tag. * **Event Name:** `purchase` * **Event Parameters:** * Parameter Name: `transaction_id`, Value: `{{DLV - ecommerce.transaction_id}}` * Parameter Name: `currency`, Value: `{{DLV - ecommerce.currency}}` * Parameter Name: `value`, Value: `{{DLV - ecommerce.value}}` * Parameter Name: `items`, Value: `{{DLV - ecommerce.items}}` * **Trigger:** `CE - Purchase` * **Tag: GA4 - Event - ViewItemList** * **Tag Type:** Google Analytics > GA4 Event * **Configuration Tag:** Select your `GA4 - Google Tag (Configuration)` tag. * **Event Name:** `view_item_list` * **Event Parameters:** * Parameter Name: `item_list_name`, Value: `{{DLV - ecommerce.item_list_name}}` * Parameter Name: `items`, Value: `{{DLV - ecommerce.items}}` * **Trigger:** `CE - View Item List` * **Tag: GA4 - Event - ViewCart** * **Tag Type:** Google Analytics > GA4 Event * **Configuration Tag:** Select your `GA4 - Google Tag (Configuration)` tag. * **Event Name:** `view_cart` * **Event Parameters:** * Parameter Name: `currency`, Value: `{{DLV - ecommerce.currency}}` * Parameter Name: `value`, Value: `{{DLV - ecommerce.value}}` * Parameter Name: `items`, Value: `{{DLV - ecommerce.items}}` * **Trigger:** `CE - View Cart` * **Tag: GA4 - Event - SelectItem** * **Tag Type:** Google Analytics > GA4 Event * **Configuration Tag:** Select your `GA4 - Google Tag (Configuration)` tag. * **Event Name:** `select_item` * **Event Parameters:** * Parameter Name: `items`, Value: `{{DLV - ecommerce.items}}` * **Trigger:** `CE - Select Item` * **Tag: GA4 - Event - AddShippingInfo** * **Tag Type:** Google Analytics > GA4 Event * **Configuration Tag:** Select your `GA4 - Google Tag (Configuration)` tag. * **Event Name:** `add_shipping_info` * **Event Parameters:** * Parameter Name: `currency`, Value: `{{DLV - ecommerce.currency}}` * Parameter Name: `value`, Value: `{{DLV - ecommerce.value}}` * Parameter Name: `shipping_tier`, Value: `{{DLV - ecommerce.shipping_method}}` * Parameter Name: `items`, Value: `{{DLV - ecommerce.items}}` * **Trigger:** `CE - Add Shipping Info` * **Tag: GA4 - Event - AddPaymentInfo** * **Tag Type:** Google Analytics > GA4 Event * **Configuration Tag:** Select your `GA4 - Google Tag (Configuration)` tag. * **Event Name:** `add_payment_info` * **Event Parameters:** * Parameter Name: `currency`, Value: `{{DLV - ecommerce.currency}}` * Parameter Name: `value`, Value: `{{DLV - ecommerce.value}}` * Parameter Name: `payment_type`, Value: `{{DLV - ecommerce.payment_method}}` * Parameter Name: `items`, Value: `{{DLV - ecommerce.items}}` * **Trigger:** `CE - Add Payment Info` After creating all tags, submit and publish your GTM container. == Changelog == = 3.3.0 = * New: REST API endpoint `/wp-json/mcapi/v1/event` for cache-safe browser-side tracking. No nonce required, so events continue to fire on pages served from a 7-day LiteSpeed/Varnish/Cloudflare cache. Secured by strict same-origin check, per-IP rate limit (50/min), global rate limit (1000/min), 16 KB body cap, and event-name whitelist. * New: Inline bootstrap helper printed at top of `
` (priority 1). All three inline tracking scripts (PageView, ViewContent, ViewCategory) share one code path that tries REST first, falls back to admin-ajax.php with a hard-coded fallback nonce if REST is blocked by a WAF. * Improvement: Reliable retries — transient API failures (HTTP 5xx, 429, network timeouts) no longer drop events from the queue. They stay queued and retry on the next cron tick. Permanent failures (4xx) still drop to avoid infinite retries. Queue retention extended to 24 hours to cover longer API outages. * Improvement: ViewCart and ViewCategory events now respect the per-event enable checkboxes in settings. They were previously always-on. * Improvement: `external_id` is now SHA-256 hashed when sent to Meta (was sent plain, which hurt Event Match Quality). * Improvement: REST handler respects per-event enable checkboxes — prevents cached pages from sending events the merchant has turned off. * Improvement: Description rewritten to highlight cache compatibility, REST/AJAX dual path, and retry semantics. * Fix: Nested `