Track SKU products or subscriptions separately in Shopify


SKU tracking

You can create a goal that would fire only when a product with a certain SKU is purchased.

For this, you would need to install the following JavaScript code in the same way that the manual revenue tracking is installed on Shopify. You could append the code along the manual revenue tracking code, making a goal for general revenue tracking and one for a specific SKU. 

You would only need to change the id, to the revenue goal that you create for this on the Convert app.

<script>
total_sku_revenue = 0;
total_sku_products = 0;

function reportRevenue(revenue,product_count) {
    _conv_q = window._conv_q || [];
    // Replace the number for your Revenue Goal Id
    _conv_q.push(["pushRevenue", revenue, product_count, "100134184"]);
    console.log('SKU Specific Report:revenue='+revenue+': Product Count='+product_count);
}
Shopify.checkout.line_items.forEach(product => {
  if (product.sku.includes('rose')){
    total_sku_revenue = product.line_price + total_sku_revenue;      
    total_sku_products =  1 + total_sku_products;
  }
})

if (total_sku_products > 0) {
reportRevenue(total_sku_revenue,total_sku_products)
}
total_sku_revenue = 0;
total_sku_products = 0;

function reportRevenue(revenue,product_count) {
    _conv_q = window._conv_q || [];
    // Replace the number for your Revenue Goal Id
    _conv_q.push(["pushRevenue", revenue, product_count, "100134184"]);
    console.log('SKU Specific Report:revenue='+revenue+': Product Count='+product_count);
}
Shopify.checkout.line_items.forEach(product => {
  if (product.sku.includes('rose')){
    total_sku_revenue = product.line_price + total_sku_revenue;      
    total_sku_products =  1 + total_sku_products;
  }
})

if (total_sku_products > 0) {
reportRevenue(total_sku_revenue,total_sku_products)
}
</script>

Subscription Tracking

To separate subscriptions from non-subscriptions you can use the following code. Make sure you add a separate revenue goal for subscriptions and non subscriptions.

<script>

document.addEventListener("DOMContentLoaded", function() {

convertRevenueCurrency = false;
currencyToConvertTo = 'USD';

function reportRevenue(revenue, product_count, goal_id, purchase_type, Currency) {
_conv_q = window._conv_q || [];
// Enable next line if currency conversion is needed.
if (convertRevenueCurrency) {
revenue = Currency.convert(revenue, Shopify.checkout.currency, currencyToConvertTo);
}
_conv_q.push(["pushRevenue", revenue, product_count, goal_id]);
console.log(purchase_type + ' Report:revenue=' + revenue + ': Product Count=' + product_count);
}

const xhr = new XMLHttpRequest();
xhr.open('GET', 'https://cdn.shopify.com/s/javascripts/currencies.js');
xhr.onload = function() {
console.log('xhr status' + xhr.status);
if (xhr.status === 200) {
eval(xhr.responseText);
total_subs_revenue = 0;
total_subs_products = 0;
total_non_subs_revenue = 0;
total_non_subs_products = 0;

Shopify.checkout.line_items.forEach(product => {
// Following line checks if it is a subscribed product by query a specific property of the product
if (product.selling_plan_allocation.selling_plan.recurring_deliveries)
total_subs_revenue = product.line_price - product.line_level_total_discount + total_subs_revenue;
total_subs_products = 1 + total_subs_products;
} else {
total_non_subs_revenue = product.line_price - product.line_level_total_discount + total_non_subs_revenue;
total_non_subs_products = 1 + total_non_subs_products;
}
})

if (total_subs_products > 0) {
// Change goal id to the one created for subscriptions
reportRevenue(total_subs_revenue, total_subs_products, '10044705', 'subscriptions', Currency);
}
if (total_non_subs_products > 0) {
// Change goal id to the one created for non subscriptions
reportRevenue(total_non_subs_revenue, total_non_subs_products, '10044706', 'non subscription', Currency);
}
}
};
xhr.send();
});
</script>

Tracking Subscriptions within Shopify Customer Events

You can substitute your Shopify Customer Event code to separate subscriptions from non-subscriptions.

/* This the code for separating subscription from non subscriptions */
/* You will need to create a goal for each of them and specify it next */
/* This will rely on the product.selling_plan_allocation.selling_plan.recurring_deliveries. */
/* This fiel may vary across subscription apps. Make sure you check which is the more appropiate one*/

// Edit the following goal ids to match your own created goals
subs_purchase_goalid = '10044705';
non_subs_purchase_goalid = '10044706';
addToCart_goalid = '100134910';

function postTransaction (convert_attributes_str){
var convert_attributes = JSON.parse(convert_attributes_str);

if (convert_attributes) {

// Determine Subscription and Non Subscription Revenue
total_subs_revenue = 0;
total_subs_products = 0;
total_non_subs_revenue = 0;
total_non_subs_products = 0;

purchase_event.data.checkout.line_items.forEach(product => {
// Following line checks if it is a subscribed product by query a specific property of the product
if (product.selling_plan_allocation.selling_plan.recurring_deliveries)
total_subs_revenue = product.line_price - product.line_level_total_discount + total_subs_revenue;
total_subs_products = 1 + total_subs_products;
} else {
total_non_subs_revenue = product.line_price - product.line_level_total_discount + total_non_subs_revenue;
total_non_subs_products = 1 + total_non_subs_products;
}
})

//build POST data to be sent
const post = {
'cid': convert_attributes.cid,
'pid': convert_attributes.pid,
'seg': convert_attributes.defaultSegments,
's': 'shopify',
'vid': convert_attributes.vid,
'ev': [
{
'evt': 'tr',
'exps': convert_attributes.exps,
'vars': convert_attributes.vars,

'r': parseFloat(total_subs_revenue),
'prc': total_subs_products
},
{
'evt': 'hitGoal',
'goals': [subs_purchase_goalid],
'exps': convert_attributes.exps,
'vars': convert_attributes.vars
}
]
}

// First add the revenue and product count in case there are subscriptions
if (total_subs_products > 0) {
post['r'] = total_subs_revenue;
post['prc'] = total_subs_products;
post['goals'] = [subs_purchase_goalid];
}
// Else adds the revenue and product count in case there are non subscriptions
else {
post['r'] = total_non_subs_revenue;
post['prc'] = total_non_subs_products;
post['goals'] = [non_subs_purchase_goalid];
},




const data = JSON.stringify(post);
console.log('Data:'+data);
browser.sendBeacon(`https://${convert_attributes.pid}.metrics.convertexperiments.com/track`, data);
}
}

function postConversion (convert_attributes_str,goalid){
console.log('Convert: Triggering Add to Cart Shopify Customer Event with goal id:'+goalid)
var convert_attributes = JSON.parse(convert_attributes_str);

if (convert_attributes) {

//build POST data to be sent
const post = {
'cid': convert_attributes.cid,
'pid': convert_attributes.pid,
'seg': convert_attributes.defaultSegments,
's': 'shopify',
'vid': convert_attributes.vid,
'ev': [
{
'evt': 'hitGoal',
'goals': [goalid],
'exps': convert_attributes.exps,
'vars': convert_attributes.vars
}
]
}
const data = JSON.stringify(post);
console.log('Data:'+data);
browser.sendBeacon(`https://${convert_attributes.pid}.metrics.convertexperiments.com/track`, data);
}
}

analytics.subscribe("checkout_completed", async (event) => {
purchase_event = event;
browser.localStorage.getItem('convert_attributes').then(postTransaction,console.log('Convert localStorage retrieval failed.'));
});

analytics.subscribe("product_added_to_cart", async (event) => {
browser.localStorage.getItem('convert_attributes')
.then((result) => postConversion(result, addToCart_goalid))
.catch((error) => {
console.log('Convert localStorage retrieval failed.')
});
});
Need assistance? Start a conversation with our Support Team.
CONVERT MORE

Reliably Convert More Site Visitors, Even If Everything Else is Changing in Your Business.

START 15-DAY FREE TRIAL
✓ No Credit Card  ✓ Easy Set-Up  ✓ Access All Features

Comments