Track SKU products or subscriptions separately in Shopify

THIS ARTICLE WILL HELP YOU:



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() {
 
let convertRevenueCurrency = false;
 
let currencyToConvertTo = 'USD';
 
function reportRevenue(revenue, product_count, goal_id, purchase_type, Currency) {
 
    let _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);
 
        let total_subs_revenue = 0;
        let total_subs_products = 0;
        let total_non_subs_revenue = 0;
        let total_non_subs_products = 0;
 
        Shopify.checkout.line_items.forEach(product => {
            // Following line checks if it is a subscribed product by querying a specific property of the product
            if (product.selling_plan_allocation && product.selling_plan_allocation.selling_plan.recurring_deliveries) {
                total_subs_revenue += product.line_price - product.line_level_total_discount;
                total_subs_products += 1;
            } else {
                total_non_subs_revenue += product.line_price - product.line_level_total_discount;
                total_non_subs_products += 1;
            }
        });
 
        if (total_subs_products > 0) {
            // Change goal id to the one created for subscriptions
            reportRevenue(total_subs_revenue, total_subs_products, '100453380', '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, '100453381', '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.