Facebook Dynamic ads pixel (AddToCart, Purchase) - Magento & Google Tag Manager

Facebook Dynamic ads pixel (AddToCart, Purchase) - Magento & Google Tag Manager

September 7, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Facebook Dynamic ads pixel (AddToCart, Purchase) - Magento & Google Tag Manager

Integrating Facebook Dynamic Ads Pixel with Magento via Google Tag Manager

In today’s digital landscape, tracking customer behavior is crucial for optimizing your e-commerce strategy. If you’re already using Google Tag Manager (GTM) for managing your site tags and want to implement Facebook’s dynamic ad pixel, this guide will walk you through the process of collecting essential events like AddToCart and Purchase directly from your Magento store.

Understanding the Problem: Capturing SKUs and Cart Value

To leverage Facebook’s dynamic product ads effectively, you need to send accurate product data (SKU) and cart value when a customer adds items or makes a purchase. Your current pixel code snippet is correctly formatted but lacks the actual values pulled from your Magento store.

The Solution: Leveraging Google Tag Manager Variables

Google Tag Manager allows you to create custom variables that extract data from the page, making it an ideal bridge between Magento and Facebook’s pixel. To achieve this, we’ll use a combination of DataLayer variables and GTM’s built-in functions.

Step 1: Setting Up the DataLayer in Magento

First, ensure your Magento store is configured to push relevant data into the DataLayer when AddToCart and Purchase events occur. You can achieve this using third-party extensions or custom scripts.

For instance, you can use the following script to push cart contents into the DataLayer:

$cartHelper = $this->helper('checkout/cart');
$items = $cartHelper->getItems();
foreach ($items as $item) {
    echo "<script>
        dataLayer.push({
            'ecommerce': {
                'addToCart': {
                    'actionField': {'list': 'ShoppingCart'},
                    'products': [{
                        'id': '" . $item->getData('sku') . "',
                        'name': '" . $item->getName() . "',
                        'price': " . $item->getPrice() . ",
                        'brand': 'YourBrand'
                    }]
                }
            }
        });
    </script>";
}

Step 2: Creating DataLayer Variables in GTM

In your Google Tag Manager account, navigate to the “Variables” section and create new DataLayer variables for the ‘ecommerce.addToCart.products.id’ (SKU) and ‘ecommerce.addToCart.products.price’ (product price).

Step 3: Building the Facebook Pixel Trigger

Next, create a new trigger in GTM that fires when the AddToCart or Purchase event is pushed into the DataLayer. You can use the “Custom Event” trigger type for this purpose.

Step 4: Constructing the Facebook Pixel Tag

Finally, update your Facebook pixel code snippet to dynamically retrieve values from the DataLayer variables you’ve created:

<script>
fbq('track', 'AddToCart', {
id: {{DataLayer - ecommerce.addToCart.products.id}},
name: '{{Product Name}}',
price: {{DataLayer - ecommerce.addToCart.products.price}},
currency: 'USD',
brand: 'YourBrand'
});
</script>

For the Purchase event, you can create a similar setup using the ‘ecommerce.purchase.actionField.value’ and ‘ecommerce.purchase.products.id’ variables.

Conclusion

Integrating Facebook’s dynamic ad pixel with your Magento store through Google Tag Manager enables you to collect valuable user behavior data without modifying your store’s core files. By following this step-by-step guide, you can now accurately track AddToCart and Purchase events, passing essential product information like SKUs and cart value to Facebook for personalized advertising.

Remember, keeping your tracking codes up-to-date is an ongoing process. Regularly review your GTM setup and adjust variables as needed to ensure optimal performance. For more advanced Magento tracking strategies, consider exploring the Laravel Company’s e-commerce development services.

Checkout Catalog Performance Extensions

Our network