Follow the steps below to integrate BigCommerce with ProfitMetrics via Zapier
Part 1/3: Create a trigger
- Log in to https://zapier.com or create an account if you do not have one.
(Up to 100 tasks per month can be created using the free Zapier account.) - Click "Create"
- Click "New Zap"
- Click the "Pen icon" in the top left corner to edit the name
- Type (or copy/paste):
ProfitMetrics - Serverside Hybrid Universal Integration
Click anywhere to save - Click "1. Trigger"
- Search for "BigCommerce" and click it.
NOTE: If this is the first time connecting to BigCommerce you will need to follow the steps to complete the connection
Please note that only account owners can create the new API Account that will be required to proceed! - Click the "Event (required)", search for "Order" and select "Order Created"
- Click "Continue", "Continue" and "Continue" again.
- Click "Test trigger"
- Click "Continue with the selected record"
Part 2/3: Create a "Code by Zapier" action
- Search for "Code" and click "Code by Zapier"
- Click "Event" and select "Run Javascript"
- Click "Continue"
- Click the field named "Input Data" and type "pid"
- Click the field next to it and insert your "Public ID" (How to find your Public ID)
- Click the "+ icon" to add a new field
- Type "id" (lower case) in the first field.
- Click "Enter text or insert data..." to add a value
- Select "Id" from the dropdown list of options
- Repeat steps 6-9 and add the following pairs of Input Data and Value.
You can copy/paste the values from the list below.
NOTE: Use the image below for reference
Input Data Value pid ProfitMetrics Public ID id ID dateCreated Date Created orderEmail Shipping Addresses Email customerPhone Shipping Addresses Phone shippingMethod Shipping Addresses Shipping Method shippingCountry Shipping Addresses Country Iso 2 shippingAddressesZip Shipping Addresses Zip paymentMethod Payment Method currencyCode Currency Code couponsCode Coupons priceShippingExVat Shipping Addresses Cost Ex Tax priceTotalExVat Total Ex Tax priceTotalInclVat Total Inc Tax productId products Variant Id OR products Product Id OR products SKU IMPORTANT: Please verify which field is to use based on the ID currently used in your product feed as "id:" or "g:id" productQuantity Products Quantity productPriceExVat Products Price Ex Tax productWeight Products Weight - Copy this script and paste it into the "Code (required)" field. Make sure to delete the existing content first.
// Convert date to unix
function convertToUnixTime(dateString) {
const date = new Date(dateString);
return date.getTime() / 1000;
}
const unixTime = convertToUnixTime(inputData['dateCreated']);
console.log('Unix Time:', unixTime);
// Calculate sum of product weight considering quantity
function sumProductWeights(productId, productQuantity, productWeight) {
const idArray = productId ? productId.split(',') : [];
const quantityArray = productQuantity ? productQuantity.split(',') : [];
const weightArray = productWeight ? productWeight.split(',') : [];
let totalWeight = 0;
for (let i = 0; i < idArray.length; i++) {
const qty = Number(quantityArray[i]);
const weight = Number(weightArray[i]);
if (!isNaN(qty) && !isNaN(weight)) {
totalWeight += qty * weight;
}
}
return totalWeight; // unknown unit
}
const shippingWeightSum = sumProductWeights(inputData['productId'], inputData['productQuantity'], inputData['productWeight']);
console.log('Shipping Weight Sum (including product quantity):', shippingWeightSum);
// Create product array
const productId = inputData['productId'];
const productQuantity = inputData['productQuantity'];
const productPriceExVat = inputData['productPriceExVat'];
const products = [];
const idArray = productId ? productId.split(',') : [];
const quantityArray = productQuantity ? productQuantity.split(',') : [];
const priceArray = productPriceExVat ? productPriceExVat.split(',') : [];
for (let i = 0; i < idArray.length; i++) {
products.push({
"sku": idArray[i],
"qty": Number(quantityArray[i]),
"priceExVat": Number(priceArray[i])
});
}
// Remove '+' sign from the phone number
const cleanedPhoneNumber = inputData['customerPhone'] ? inputData['customerPhone'].replace(/\+/g, '') : '';
console.log('Cleaned Phone Number:', cleanedPhoneNumber);
// Create JSON object with omitted undefined values
const data = {
"id": inputData['id'],
"ts": unixTime,
"orderEmail": inputData['orderEmail'],
"customerPhone": cleanedPhoneNumber,
"shippingMethod": inputData['shippingMethod'],
"shippingCountry": inputData['shippingCountry'],
"shippingZipcode": `${inputData['shippingAddressesZip']}`,
"shippingWeight": Number(shippingWeightSum),
"paymentMethod": inputData['paymentMethod'],
"currency": inputData['currencyCode'],
"voucherCode": inputData['couponsCode'],
"priceShippingExVat": Number(inputData['priceShippingExVat']),
"priceTotalExVat": Number(inputData['priceTotalExVat']),
"priceTotalInclVat": Number(inputData['priceTotalInclVat']),
"products": products
};
// Filter out undefined values
const filteredData = Object.fromEntries(Object.entries(data).filter(([_, v]) => v !== undefined));
const jsonData = JSON.stringify(filteredData);
console.log('JSON Data:', jsonData);
const url = 'https://my.profitmetrics.io/l.php?v=3uh&pid=' + inputData['pid'] + '&o=' + encodeURIComponent(jsonData);
await fetch(url)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
callback(null, { "URL": + url}); - Click "Continue", "Continue" and "Continue"
- Click "Publish"
Part 3/3: Insert the ProfitMetrics tracking in BigCommerce
Create the default tracking script
- Log in to BigCommerce.com
- Click Channel Manager
- Click Scripts
- Click Create a script
- In Script name type "ProfitMetrics"
- In Description type "Default ProfitMetrics script"
- Under placement, select Footer
- Under Location, select All pages
- Under script category, select Essential
- Under script type, select Script
- .Paste in the following script and replace both instances of "PUBLIC_ID" with your Public ID (found here)
<!-- ProfitMetrics snippet START-->
<script>
window.profitMetrics = {
pid: 'PUBLIC_ID', // The ProfitMetrics website ID
cookieMarketingConsent: false,
cookieStatisticsConsent: false,
}
</script>
<script src="https://cdn1.profitmetrics.io/PUBLIC_I/bundle.js"
defer></script>
<!-- ProfitMetrics snippet END--> - Click Save
Create the Marketing consent script
- Click Create a script
- In Script name type "ProfitMetrics - Marketing Consent"
- In Description type "Set marketing consent for ProfitMetrics script"
- Under placement, select Footer
- Under Location, select All pages
- Under script category, select Targeting/Advertising
- Under script type, select Script
- Paste in the following script and replace "PUBLIC_ID" with your Public ID
(found here)<!-- ProfitMetrics snippet START-->
<script>
window.profitMetrics.cookieMarketingConsent = true;
</script>
<script src="https://cdn1.profitmetrics.io/PUBLIC_ID/bundle.js"
defer>
</script>
<!-- ProfitMetrics snippet END--> - Click Save
Create the Analytics consent script
- Click Create a script
- In the Script name type "ProfitMetrics - Analytics Consent"
- In the Description type "Set marketing consent for ProfitMetrics script"
- Under placement, select Footer
- Under Location, select All pages
- Under the script category, select Analytics
- Under script type, select Script
- .Paste in the following script and replace "PUBLIC_ID" with your Public ID
(found here) -
<!-- ProfitMetrics snippet START-->
<script>
window.profitMetrics.cookieStatisticsConsent = true;
</script>
<script src="https://cdn1.profitmetrics.io/PUBLIC_ID/bundle.js"
defer>
</script>
<!-- ProfitMetrics snippet END--> -
.Click Save
Create a setEmail script
- Navigate to Settings
- Scroll down to "Advanced" and click Data solutions
- Find "Affiliate Conversion Tracking" and click Connect
- Insert the following script
<!-- Start ProfitMetrics - setEmail -->
<script>
function _pm_tryemailorderconfirmation() {
if( null != window.profitMetrics && null != window.profitMetrics.setEmail && typeof window.profitMetrics.setEmail === 'function' ) {
window.profitMetrics.setEmail( '%%ORDER_EMAIL%%' );
} else {
setTimeout( _pm_tryemailorderconfirmation, 500 );
}
}
_pm_tryemailorderconfirmation();
</script>
<!-- End ProfitMetrics - setEmail --> -
Click Save