Improving Attribution Accuracy for Facebook Ads in osCommerce

Standard

🎯 Objective

Enhance attribution accuracy by:

  • Combining Meta Pixel and CAPI
  • Implementing event_id-based deduplication
  • Capturing richer user data (email, IP, user agent)
  • Mitigating data loss from iOS, ad blockers, and browser restrictions

✅ Tools & Requirements

Tool Purpose
Facebook Pixel & Business Manager Campaigns & tracking
Facebook Conversions API Access Token For server-side events
Google Tag Manager (Web + Server) Event forwarding & CAPI
osCommerce access Inject dataLayer, product/order data
Cloud Hosting (GCP recommended) Host sGTM

🔧 Implementation Plan

  1. Install GTM Web + Server on osCommerce
  2. Fire Meta Pixel events via GTM Web
  3. Push purchase and lead data into dataLayer
  4. Forward data to sGTM
  5. Send deduplicated events via Facebook CAPI
  6. Add SHA256 email hashing
  7. Validate attribution using Meta Events Manager

🧱 Step-by-Step Implementation


🔹 1. Add GTM Web & Server in osCommerce

a. GTM Web Container

Insert into:

  • <head>: includes/template_top.php
  • <body>: noscript iframe version

b. GTM Server Container

  • Create in GTM
  • Deploy via GCP App Engine or Cloud Run:
gcloud app deploy
  • Save endpoint: https://gtm.yourstore.com

🔹 2. Inject Purchase Data with event_id in checkout_success.php

<?php
$order_query = tep_db_query("SELECT orders_id, order_total, customers_email_address FROM " . TABLE_ORDERS . " WHERE customers_id = '" . (int)$customer_id . "' ORDER BY orders_id DESC LIMIT 1");
$order = tep_db_fetch_array($order_query);
$order_id = $order['orders_id'];
$order_total = $order['order_total'];
$customer_email = $order['customers_email_address'];
$event_id = bin2hex(random_bytes(16)); // Unique event ID
?>
<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
event: 'purchase',
transaction_id: '<?= $order_id ?>',
value: <?= $order_total ?>,
currency: 'USD',
email: '<?= $customer_email ?>',
event_id: '<?= $event_id ?>'
});
</script>

🔹 3. GTM Web – Meta Pixel Tag (Client-Side)

a. Trigger: Custom Event = purchase

b. Tag Type: Custom HTML

<script>
fbq('track', 'Purchase', {
  value: {{DLV - value}},
  currency: '{{DLV - currency}}',
  eventID: '{{DLV - event_id}}'
});
</script>

🔹 4. GTM Web – Forward Data to Server GTM

a. Tag: HTTP Request Tag

  • URL: https://gtm.yourstore.com/collect
  • Method: POST
  • Payload:
{
  "event_name": "Purchase",
  "event_id": "{{DLV - event_id}}",
  "transaction_id": "{{DLV - transaction_id}}",
  "value": {{DLV - value}},
  "currency": "{{DLV - currency}}",
  "email": "{{DLV - email}}",
  "user_agent": "{{User-Agent}}",
  "ip_override": "{{Client IP}}"
}

🔹 5. GTM Server – Facebook CAPI Tag with Deduplication

a. Variables in sGTM:

  • event_name, event_id, transaction_id, value, currency, email, user_agent, ip_override

b. Tag: Custom Template or Custom JavaScript

const sendHttpRequest = require('sendHttpRequest');
const log = require('logToConsole');
const JSON = require('JSON');

const access_token = 'YOUR_FACEBOOK_ACCESS_TOKEN';
const pixel_id = 'YOUR_PIXEL_ID';

const payload = {
data: [{
event_name: data.event_name,
event_time: Math.floor(Date.now() / 1000),
event_id: data.event_id,
action_source: 'website',
user_data: {
em: [sha256(data.email.trim().toLowerCase())],
client_ip_address: data.ip_override,
client_user_agent: data.user_agent
},
custom_data: {
value: data.value,
currency: data.currency,
order_id: data.transaction_id
}
}]
};

sendHttpRequest(
`https://graph.facebook.com/v18.0/${pixel_id}/events?access_token=${access_token}`,
{
method: 'POST',
headers: {'Content-Type': 'application/json'}
},
JSON.stringify(payload)
);

log("✅ Facebook CAPI Purchase sent");

🔹 6. Add SHA256 Hash Function (Server-Side)

If using a custom template:

function sha256(str) {
return Utilities.computeDigest(
Utilities.DigestAlgorithm.SHA_256,
str,
Utilities.Charset.UTF_8
).map(b => ('0' + (b & 0xFF).toString(16)).slice(-2)).join('');
}

🔹 7. Test Deduplication & Attribution Accuracy

✅ Tools:

  • Meta Events Manager > Test Events
  • Meta Pixel Helper
  • Confirm:
    • Pixel fires with eventID
    • CAPI fires with same eventID
    • Only one Purchase counted

📊 Pro Tips to Improve Attribution Accuracy Further

Technique Description
✅ Use event_id Enables deduplication between Pixel & CAPI
✅ Send hashed PII Improves user matching
✅ Use user IP & user agent Boosts signal quality
✅ Trigger CAPI even when Pixel is blocked Covers privacy-related loss
✅ Set up Consent Mode Aligns with regulations like GDPR/CCPA

🧠 Summary

Step Purpose
Inject dataLayer on purchase Capture transaction data
Fire Pixel + send event_id Track client-side events
Forward data to sGTM Trigger server-side tracking
Post to CAPI with hashed data Improve signal quality
Deduplicate using event_id Avoid double counting

Leave a Reply

Your email address will not be published. Required fields are marked *