How to integrate BigCommerce with ProfitMetrics using Zapier

Follow the steps below to integrate BigCommerce with ProfitMetrics via Zapier

  1. Create a trigger
  2. Create a "Code by Zapier" action
  3. Insert the ProfitMetrics tracking

Part 1/3: Create a trigger

  1. 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.)
  2. Click "Create"
    Zapier Workflow - Step 2
  3. Click "New Zap"
    How to Create a Zap on Zapier - Step 3
  4. Click the "Pen icon" in the top left corner to edit the name
    Creating a ProfitMetrics - Serverside Hybrid Universal Integration - Step 2
  5. Type (or copy/paste):
    ProfitMetrics - Serverside Hybrid Universal Integration  
    Click anywhere to save
    Creating a ProfitMetrics - Serverside Hybrid Universal Integration - Step 4
  6. Click "1. Trigger"
    How to Create a Zap on Zapier - Step 4
  7. 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!


    How to Create a Zap on Zapier - Step 8
  8. Click the "Event (required)", search for "Order" and select "Order Created"
    How to Create a Zap on Zapier - Step 11
  9. Click "Continue", "Continue" and "Continue" again.
    How to Create a Zap on Zapier - Step 12
    How to Create a Zap on Zapier - Step 13
  10. Click "Test trigger"
    How to Create a Zap on Zapier - Step 18
  11. Click "Continue with the selected record"

How to Create a Zap on Zapier - Step 19

back to top

Part 2/3: Create a "Code by Zapier" action

  1. Search for "Code" and click "Code by Zapier"How to Create a Zap on Zapier - Step 21
  2. Click "Event" and select "Run Javascript"How to Create a Zap on Zapier - Step 23 (1)
  3. Click "Continue"How to Create a Zap on Zapier - Step 24
  4. Click the field named "Input Data" and type "pidGuide to Editing Specific Text Field Using Keyboard Shortcuts - Step 4
  5. Click the field next to it and insert your "Public ID" (How to find your Public ID)
    Guide to Editing Specific Text Field Using Keyboard Shortcuts - Step 10
  6. Click the "+ icon" to add a new field
    Accessing and navigating Zapier editor for specific draft. - Step 2
  7. Type "id" (lower case) in the first field. 
    Set up Zapier connection with _id_ integration. - Step 4
  8. Click "Enter text or insert data..." to add a value
    Set up Zapier connection with _id_ integration. - Step 6
  9. Select "Id" from the dropdown list of options
    Set up Zapier connection with _id_ integration. - Step 9
  10. 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
    Google Workflow - Step 1
  11. 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});
    How to Copy and Paste Text on Zapier - Step 2
  12. Click "Continue", "Continue" and "Continue"
    How to Copy and Paste Text on Zapier - Step 7
    How to Copy and Paste Text on Zapier - Step 9
  13. Click "Publish"How to Publish and Test a Webhook Integration - Step 33

back to top

Part 3/3: Insert the ProfitMetrics tracking in BigCommerce

Create the default tracking script

  1. Log in to BigCommerce.com
  2. Click Storefront
    Installing ProfitMetrics Scripts on BigCommerce Store - Step 4 (1)
  3. Click Scripts
    Installing ProfitMetrics Scripts on BigCommerce Store - Step 5
  4. Click Create a script
    Installing ProfitMetrics Scripts on BigCommerce Store - Step 6
  5. In Script name type "ProfitMetrics - Script"
    Installing ProfitMetrics Scripts on BigCommerce Store - Step 10
  6. Ensure Placement is set to Footer
    Installing ProfitMetrics Scripts on BigCommerce Store - Step 11
  7. Under Location, select All pages
    Installing ProfitMetrics Scripts on BigCommerce Store - Step 12
  8. Ensure Placement is set to Footer
    Installing ProfitMetrics Scripts on BigCommerce Store - Step 13
  9. Under script type, select Script
    Installing ProfitMetrics Scripts on BigCommerce Store - Step 14
  10. Paste in the following script and replace both instances of "PUBLIC_ID" with your Public ID (found here)
    <!-- Start ProfitMetrics - Script (V1) -->
    <script>
    window.profitMetrics = {
    pid: 'PUBLIC_ID', // The ProfitMetrics website ID
    cookieMarketingConsent: false,
    cookieStatisticsConsent: false,
    }
    </script>

    <script src="https://cdn1.profitmetrics.io/PUBLIC_ID/bundle.js"
    defer></script>
    <!-- End ProfitMetrics - Script -->
  11. Click Save

Create the Marketing consent script

  1. Click Create a script
    Installing ProfitMetrics Scripts on BigCommerce Store - Step 6
  2. In Script name type "ProfitMetrics - Marketing Consent"
    Installing ProfitMetrics Scripts on BigCommerce Store - Step 51
  3. Ensure Placement is set to Footer
    Installing ProfitMetrics Scripts on BigCommerce Store - Step 11
  4. Under Location, select All pages
    Installing ProfitMetrics Scripts on BigCommerce Store - Step 12
  5. Under script category, select Targeting/Advertising
    Installing ProfitMetrics Scripts on BigCommerce Store - Step 54-1
  6. Under script type, select Script
    Installing ProfitMetrics Scripts on BigCommerce Store - Step 14
  7. Paste in the following script and replace "PUBLIC_ID" with your Public ID
    (found here)
    <!-- Start ProfitMetrics - Marketing Consent -->
    <script>
    window.profitMetrics.cookieMarketingConsent = true;
    </script>
    <script src="https://cdn1.profitmetrics.io/PUBLIC_ID/bundle.js"
    defer>
    </script>
    <!-- End ProfitMetrics - Marketing Consent -->
  8. Click Save

Create the Analytics consent script

  1. Click Create a script
    Installing ProfitMetrics Scripts on BigCommerce Store - Step 6
  2. In the Script name type "ProfitMetrics - Analytics Consent"
    Editing a Script in BigCommerce Store Manager - Step 4
  3. Ensure Placement is set to Footer
    Installing ProfitMetrics Scripts on BigCommerce Store - Step 11
  4. Under Location, select All pages
    Installing ProfitMetrics Scripts on BigCommerce Store - Step 12
  5. Under the script category, select Analytics
    Accessing Analytics in BigCommerce Dashboard - Step 3
  6. Under script type, select Script
    Installing ProfitMetrics Scripts on BigCommerce Store - Step 14
  7. .Paste in the following script and replace "PUBLIC_ID" with your Public ID 
    (found here)
  8. <!-- Start ProfitMetrics - Statistics Consent -->
    <script>
    window.profitMetrics.cookieStatisticsConsent = true;
    </script>
    <script src="https://cdn1.profitmetrics.io/PUBLIC_ID/bundle.js"
    defer>
    </script>
    <!-- End ProfitMetrics - Statistics Consent -->
  9. .Click Save

Create a setEmail script 

  1. Navigate to Settings
    Installing ProfitMetrics Scripts on BigCommerce Store - Step 93
  2. Scroll down to "Advanced" and click Data solutions
    Installing ProfitMetrics Scripts on BigCommerce Store - Step 94
  3. Find "Affiliate Conversion Tracking" and click Connect
    Installing ProfitMetrics Scripts on BigCommerce Store - Step 95
  4. Insert the following script
    <!-- Start ProfitMetrics - setEmail (V1) -->
    <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 -->
  5. Click Save
    Installing ProfitMetrics Scripts on BigCommerce Store - Step 98

That's it.