Facebook Conversion APi-Purchase Code Required

Is there anyone who can help me with the purchase event? Anyone help me with coding, I have been struggling for a long time. I am looking to set up a conversion API for the Shopify store. I have all events except the purchase events. I have set up the tags and variables. Check out this recording

[Google Tag Manager](https://Click here)

and here is the code i am using:

If (type === “Purchase”) {
dataLayer.push({
‘event’: type === “purchase” ? “CAPI Purchase” : “Failed”,
‘event_id’: eventId,
‘price’: parseFloat((data[“cd[value]”])).toFixed(2),
‘orderIds’: data[“cd[content_ids]”],
‘itemCount’: parseInt(data[“cd[num_items]”]),
‘currency’: data[“cd[currency]”],
‘contentType’: data[“cd[content_type]”],
‘customer_id’: ShopifyAnalytics.meta.page.customerId
? ShopifyAnalytics.meta.page.customerId
: “”,
});
}

Following. Can anyone please answer.

Without Shopify Plus you don’t have access to the checkout pages (GTM doesn’t work there), this makes it quite difficult to track the purchase event with complete data.

You can do the following. Add the following code to Settings → Checkout → Order processing → Additional scripts:

<script>
    window.dataLayer = window.dataLayer || [];
    {%- if customer -%}
        dataLayer.push({
            'event': 'customer_data',
            'id': {{customer.id | json }},
            'firstName': {{ customer.first_name | json }},
            'surname': {{ customer.last_name | json }},
            'email': {{ customer.email | json }},
            'ordersCount': {{ customer.orders_count | json }},
            'totalSpent': {{ customer.total_spent | money_without_currency | json }},
            'acceptsMarketing': {{ customer.accepts_marketing | json }},
            'tags': {{ customer.tags | json }}
        });
    {%- endif -%}
</script>

Now, after payment, you will have customer_data event in dataLayer with user data.Using this data you can make a purchase event, but unfortunately without a list of your purchased items (because you can’t access the checkout pages without shopify plus).

But for purchase event trigger I would recommend to use DOM page view, page url contains thank_you. This is because customer_data comes every time the thank you page reloads and in case of page reloads extra purchase events can come.
In case of page url trigger, the link will change after reloading, so the event won’t be duplicated unnecessarily.

2 Likes