Post-Purchase Tracking in Shopify

This article will explain the process of how to set up post purchases as upsell events inside Everflow.

peter kurjanowicz avatar
Written by peter kurjanowicz
Updated over a week ago

Overview

This article is for advanced users. If you are unsure about anything in this article please contact Everflow support and we will assist you.

Please note: You need to allow all duplicates at the Offer level, and you must have your account configured by someone on the Everflow Support team to dedupe conversions by order_id + adv1. The upsells will still track to Everflow if you do not set this up correctly, but they will appear under invalid conversions.


Step #1: Everflow Offer Setup

  • First, you must set up an Advertiser Level Event, and tie it to the appropriate Offer Level Event.
    For instructions on how to do this - [Click Here]

Step #2: Everflow Data Prep

  • Copy your Tracking Domain, Advertiser ID (aid), and Advertiser Event ID (adv_event_id) so that you may place it in the code later.

  • You can find the Advertiser ID (aid) on the General card, and the Advertiser Event ID (adv_event_id) on the Revenue & Payout card. In this example, the aid is 139 and the adv_event_id is 26.

  • Please note that your Tracking card could look different if Javascript SDK is not selected as the Conversion Method. If so, edit the the Conversion Method before moving on by navigating to Edit > Attribution > Conversion Method: Javascript SDK.

Step #3: Shopify

  • Navigate to Settings > Checkout inside Shopify.

Step #4: Shopify Order Status Code

  • Replace INSERT_TRACKING_DOMAIN and INSERT_ADVERTISER_ID with the proper values you copied in Step 2 to complete the code below:

<script type="text/javascript" src="INSERT_TRACKING_DOMAIN/scripts/sdk/everflow.js"></script>

<script type="text/javascript">
(function() {
var aid = INSERT_ADVERTISER_ID;
var amount = Shopify.checkout.subtotal_price;
var order;

if (Shopify.checkout.discount != null) {
order = {
oid: Shopify.checkout.order_id,
amt: Shopify.checkout.subtotal_price,
bs: Shopify.checkout.shipping_address ? Shopify.checkout.shipping_address.province : '',
bc: Shopify.checkout.shipping_address ? Shopify.checkout.shipping_address.country : '',
cc: Shopify.checkout.discount.code,
items: []
};
} else {
order = {
oid: Shopify.checkout.order_id,
amt: Shopify.checkout.subtotal_price,
bs: Shopify.checkout.shipping_address ? Shopify.checkout.shipping_address.province : '',
bc: Shopify.checkout.shipping_address ? Shopify.checkout.shipping_address.country : '',
cc: "",
items: []
};
}

Shopify.checkout.line_items.forEach(function (line_item) {
var item = {};
item['ps'] = line_item.sku;
item['p'] = line_item.price;
item['qty'] = line_item.quantity;
order.items.push(item);
});


EF.conversion({
aid: aid,
amount: amount,
order: order,
order_id: Shopify.checkout.order_id,
email: Shopify.checkout.email,
adv1: amount
})
})();
</script>

Here is an example of what the finished code looks like using the example values from Step #2.

<script type="text/javascript" src="https://www.serve-eflow-test.com/scripts/sdk/everflow.js"></script>

<script type="text/javascript">
(function() {
var aid = 139;
var amount = Shopify.checkout.subtotal_price;
var order;

if (Shopify.checkout.discount != null) {
order = {
oid: Shopify.checkout.order_id,
amt: Shopify.checkout.subtotal_price,
bs: Shopify.checkout.shipping_address ? Shopify.checkout.shipping_address.province : '',
bc: Shopify.checkout.shipping_address ? Shopify.checkout.shipping_address.country : '',
cc: Shopify.checkout.discount.code,
items: []
};
} else {
order = {
oid: Shopify.checkout.order_id,
amt: Shopify.checkout.subtotal_price,
bs: Shopify.checkout.shipping_address ? Shopify.checkout.shipping_address.province : '',
bc: Shopify.checkout.shipping_address ? Shopify.checkout.shipping_address.country : '',
cc: "",
items: []
};
}

Shopify.checkout.line_items.forEach(function (line_item) {
var item = {};
item['ps'] = line_item.sku;
item['p'] = line_item.price;
item['qty'] = line_item.quantity;
order.items.push(item);
});


EF.conversion({
aid: aid,
amount: amount,
order: order,
order_id: Shopify.checkout.order_id,
email: Shopify.checkout.email,
adv1: amount
})
})();
</script>

  • In Shopify, navigate to Shopify > Settings > Checkout and paste that code under Additional Scripts on the Order status page.

Step #5: Shopify Post-Purchase Code

  • Replace INSERT_TRACKING_DOMAIN, INSERT_ADVERTISER_ID and INSERT_ADV_EVENT_ID with the proper values you copied in Step 2 to complete the code below:

<script type="text/javascript">
var script = document.createElement('script');
script.onload = function() {
(function() {
var aid = INSERT_ADVERTISER_ID
var adv_event_id = INSERT_ADV_EVENT_ID
// make sure the initial conversion isn't tracked twice
if (!Shopify.wasPostPurchasePageSeen) {
var order = window.Shopify.order;
var order1 = {"oid": order.id, "items": []}
for (var i = 0; i < order.lineItems.length; i++) {
var a = {};
a.ps = order.lineItems[i].title;
a.p = order.lineItems[i].price;
a.qty = order.lineItems[i].quantity;
order1.items.push(a)
}
// track initial conversion
EF.conversion({
aid: aid,
amount: order.subtotalPrice,
order_id: order.id,
email: order.customer.email,
coupon_code: order.discounts ? order.discounts[0].code : "",
order : order1,
adv1: order.subtotalPrice
})



}

// set up additional conversion tracking
Shopify.on('CheckoutAmended', function(newOrder, previousOrder) {
var previousOrderSubtotal = previousOrder.subtotalPrice
var oldItems = previousOrder.lineItems.map(function (line) { return line.id; });

var addedItems = newOrder.lineItems.filter(
function (line) { return oldItems.indexOf(line.id) < 0; }
);

// no new items were added, so we skip conversion tracking
if (addedItems.length === 0) {
return;
}

// track additional purchase

var order = window.Shopify.order;
var amount = order.subtotalPrice - previousOrderSubtotal;
var order1 = {"oid": order.id, "items": []}
for (var i = 0; i < order.lineItems.length; i++) {
var a = {};
a.ps = order.lineItems[i].title;
a.p = order.lineItems[i].price;
a.qty = order.lineItems[i].quantity;
order1.items.push(a)
}
EF.conversion({
aid: aid,
adv_event_id: adv_event_id,
amount: amount,
order_id: order.id,
email: order.customer.email,
coupon_code: order.discounts ? order.discounts[0].code : "",
order: order1,
adv1: amount
})

});
})();
}
script.src ='https://INSERT_TRACKING_DOMAIN/scripts/sdk/everflow.js';
script.async = true;
document.head.appendChild(script);

</script>

Here is an example of what the finished code looks like using the example values from Step #2.

<script type="text/javascript">
var script = document.createElement('script');
script.onload = function() {
(function() {
var aid = 139;
var adv_event_id = 26;
// make sure the initial conversion isn't tracked twice
if (!Shopify.wasPostPurchasePageSeen) {
var order = window.Shopify.order;
var order1 = {"oid": order.id, "items": []}
for (var i = 0; i < order.lineItems.length; i++) {
var a = {};
a.ps = order.lineItems[i].title;
a.p = order.lineItems[i].price;
a.qty = order.lineItems[i].quantity;
order1.items.push(a)
}
// track initial conversion
EF.conversion({
aid: aid,
amount: order.subtotalPrice,
order_id: order.id,
email: order.customer.email,
coupon_code: order.discounts ? order.discounts[0].code : "",
order : order1,
adv1: order.subtotalPrice
})



}

// set up additional conversion tracking
Shopify.on('CheckoutAmended', function(newOrder, previousOrder) {
var previousOrderSubtotal = previousOrder.subtotalPrice
var oldItems = previousOrder.lineItems.map(function (line) { return line.id; });

var addedItems = newOrder.lineItems.filter(
function (line) { return oldItems.indexOf(line.id) < 0; }
);

// no new items were added, so we skip conversion tracking
if (addedItems.length === 0) {
return;
}

// track additional purchase

var order = window.Shopify.order;
var amount = order.subtotalPrice - previousOrderSubtotal;
var order1 = {"oid": order.id, "items": []}
for (var i = 0; i < order.lineItems.length; i++) {
var a = {};
a.ps = order.lineItems[i].title;
a.p = order.lineItems[i].price;
a.qty = order.lineItems[i].quantity;
order1.items.push(a)
}
EF.conversion({
aid: aid,
adv_event_id: adv_event_id,
amount: amount,
order_id: order.id,
email: order.customer.email,
coupon_code: order.discounts ? order.discounts[0].code : "",
order: order1,
adv1: amount
})

});
})();
}
script.src ='https://www.serve-eflow-test.com/scripts/sdk/everflow.js';
script.async = true;
document.head.appendChild(script);

</script>

  • In Shopify, navigate to Shopify > Settings > Checkout and paste that code under Additional Scripts on the Post-purchase page.


Did this answer your question?