WordPress & WooCommerce

Install Anacoic on WordPress or WooCommerce sites

WordPress & WooCommerce Integration

This guide covers installing Anacoic on WordPress sites, with special attention to WooCommerce stores.

Prerequisites

  • WordPress site (self-hosted or WordPress.com Business)
  • For WooCommerce: Plugin installed and activated
  • Anacoic account with gateway created

Method 1: Theme Header (Simplest)

Add the tracker to your theme’s header file.

Step 1: Get Your Snippet

From your Anacoic Dashboard → Gateway → Installation tab, copy the One-Line snippet:

<script src="https://edge.anacoic.com/v1/tracker.js"
        data-gateway="YOUR_GATEWAY_ID"
        data-emq="optimize"></script>

Step 2: Edit Theme Header

  1. In WordPress Admin, go to AppearanceTheme Editor
  2. Find header.php in the right sidebar
  3. Locate </head> tag
  4. Paste your snippet right before it:
<!-- Anacoic Tracking -->
<script src="https://edge.anacoic.com/v1/tracker.js"
        data-gateway="YOUR_GATEWAY_ID"
        data-emq="optimize"></script>
<?php wp_head(); ?>
  1. Click Update File

Note: Use a child theme to prevent updates from overwriting your changes.

Method 2: Insert Headers and Footers Plugin

No code editing required.

Install Plugin

  1. Go to PluginsAdd New
  2. Search for “Insert Headers and Footers”
  3. Install and activate the plugin by WPBeginner

Add Anacoic Code

  1. Go to SettingsInsert Headers and Footers
  2. Paste your snippet in the Scripts in Header box
  3. Click Save

Method 3: WPCode (Advanced)

For conditional loading and advanced features.

Install WPCode

  1. PluginsAdd New
  2. Search for “WPCode”
  3. Install and activate

Create Snippet

  1. Go to Code SnippetsAdd Snippet
  2. Click Add Your Custom Code
  3. Title: “Anacoic Tracking”
  4. Code Type: HTML Snippet
  5. Paste your One-Line snippet
  6. Location: Site Wide Header
  7. Toggle Active → ON
  8. Click Save Snippet

WooCommerce Integration

For e-commerce tracking, you need to pass order data.

Purchase Event in Thank You Page

Add to AppearanceTheme Editorfunctions.php:

// Anacoic Purchase Tracking
add_action('woocommerce_thankyou', 'anacoic_purchase_tracking', 10, 1);

function anacoic_purchase_tracking($order_id) {
    $order = wc_get_order($order_id);
    if (!$order) return;
    
    $total = $order->get_total();
    $currency = $order->get_currency();
    $email = $order->get_billing_email();
    $phone = $order->get_billing_phone();
    ?>
    <script>
        if (window.anacoic) {
            window.anacoic.track({
                event_name: 'Purchase',
                event_id: '<?php echo $order_id; ?>_' + Date.now(),
                user: {
                    email: '<?php echo esc_js($email); ?>',
                    phone: '<?php echo esc_js($phone); ?>'
                },
                custom_data: {
                    value: <?php echo $total; ?>,
                    currency: '<?php echo $currency; ?>',
                    transaction_id: '<?php echo $order_id; ?>'
                }
            });
        }
    </script>
    <?php
}

Add to Cart Tracking

// Track Add to Cart
add_action('wp_footer', 'anacoic_add_to_cart_tracking');

function anacoic_add_to_cart_tracking() {
    if (!is_product()) return;
    global $product;
    ?>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            var addButton = document.querySelector('.single_add_to_cart_button');
            if (addButton) {
                addButton.addEventListener('click', function() {
                    if (window.anacoic) {
                        window.anacoic.track({
                            event_name: 'AddToCart',
                            custom_data: {
                                content_name: '<?php echo esc_js($product->get_name()); ?>',
                                content_type: 'product',
                                value: <?php echo $product->get_price(); ?>,
                                currency: '<?php echo get_woocommerce_currency(); ?>'
                            }
                        });
                    }
                });
            }
        });
    </script>
    <?php
}

Achieving High EMQ Scores

For WooCommerce Orders

Pass all available customer data:

anacoic.track({
  event_name: 'Purchase',
  user: {
    email: customerEmail,      // From order
    phone: customerPhone,      // From order (E.164 format)
    external_id: userId        // WordPress user ID if logged in
  },
  custom_data: {
    value: orderTotal,
    currency: orderCurrency
  }
});
// Expected EMQ: 7.5-9.0

Phone Number Formatting

WooCommerce stores phone in local format. Convert to E.164:

function format_phone_e164($phone, $country) {
    $phone = preg_replace('/[^0-9]/', '', $phone);
    
    // Add country code if missing
    $country_codes = [
        'US' => '1',
        'GB' => '44',
        'DE' => '49',
        'FR' => '33',
        // Add more as needed
    ];
    
    if (isset($country_codes[$country]) && strlen($phone) <= 10) {
        $phone = $country_codes[$country] . $phone;
    }
    
    return '+' . $phone;
}

WordPress-Specific Issues

Caching Plugins

If using WP Rocket, W3 Total Cache, etc.:

  1. Exclude edge.anacoic.com from caching
  2. Don’t minify the tracker script
  3. Clear cache after adding snippet

WP Rocket Settings:

  • File OptimizationExcluded JavaScript Files: Add edge.anacoic.com
  • File OptimizationExcluded Inline JavaScript: Add anacoic

Security Plugins

Plugins like Wordfence may block external scripts.

Whitelist in Wordfence:

  1. WordfenceAll Options
  2. Whitelisted URLs: Add edge.anacoic.com

GDPR Compliance

If using GDPR plugins (CookieYes, Complianz):

  1. Categorize Anacoic as “Analytics” or “Marketing”
  2. Only load after consent:
// With CookieYes
document.addEventListener('cookieyes_consent_update', function(eventData) {
    const data = eventData.detail;
    if (data.accepted.includes('analytics')) {
        // Load Anacoic
        loadAnacoic();
    }
});

Multi-Site Network

For WordPress Multisite:

  1. Network activate the tracking code, OR
  2. Use different gateways per site:
// In wp-config.php or mu-plugin
$site_gateways = [
    1 => 'gateway_site_1',
    2 => 'gateway_site_2',
];

$current_gateway = $site_gateways[get_current_blog_id()] ?? 'default_gateway';

Next Steps

  1. Configure Meta CAPI for Facebook tracking
  2. Add custom domain for reliability
  3. Set up Agent Gateway for AI discoverability

Troubleshooting

Events Not Showing

  1. Check browser console for errors
  2. Verify gateway ID is correct
  3. Check if security plugin is blocking requests
  4. Test with caching disabled

Low EMQ Scores

  1. Ensure user.email is passed on purchase
  2. Add phone number formatting
  3. Check that customer is logged in (for external_id)

Need more help? Contact support