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 *