Search code examples
javascriptarraysgoogle-tag-managerlookup-tables

ecommerce; Howto create weighfactors to use in calculation (based on the category of products)?


When a client completes an order, steps are as follows:

  1. We will get the weighfactors based on the category for each product. (in a lookup table)
  2. Per product, the calculation will be made using the price, quantity and weigh factor
  3. The sum of all the products individual calculation will be the new conversionvalue passed on to Google Ads using the conversionpixel.

Instead of the revenue reported by our website we want to work with a different number that better represent the actual sales that go through. We want to work with new "actual" values in order to be able to use automated bidding strategies in Google Ads: For instance: phones have a 25% acceptance rate, so if 4 phones of €500 are sold, 3 of them will probably be cancelled. If the conversion value would be €125 each time- this way 4 sales will make €500 revenue together and be a good estimate of actual revenue instead of theoretical revenue.

4*500 - 3*500 = 500 = 4*125

We want to implement a new google pixel on the clients website where we want to communicate the new conversion value to Google Ads. But in order to make this happen we need several things:

-Full overview of the shoppingbaskets transaction products when someone checks out on the website. This mostly contains SKU, Name, Price, Quantity -A table that depicts the weighfactor of every category -A piece of javascript that reads out the shopping basket and calculates the new total conversionvalue based on pricequantityweighfactor for each product -A Google Ads conversion pixel in which we parse the new conversionvalue

In the site we have a piece of code when there's a conversion:
This array below is called transactionProducts:

transactionProducts: [ 
{ Sku: 'dd44', Name: 'tshirt', Category: 'apparel1', Price: 1.99, Quantity: 2, },
 { Sku: 'AA1243544', Name: 'socks', Category: 'apparel2', Price: 9.99, Quantity: 3, } 
 ] 

There's a lookup table where apparel1 converts to 0.5 and apparel2 converts to 0.2 values; So in the end my final conversionvalue should look like this;

2*1.99*0.5 + 3*9.99*0.2 = 1.99 + 5.994 = 7.984

I wanted to create a piece of javascript that makes the sum of the products for every product in the array. Below is what i got so far, but since i'm not a javascript pro i need some assistance with this.

function() { 
var sum = 0 for (var i = 0; i < {{VAR - transactionProducts}}.length; i++){
 sum += {{VAR - transactionProducts}}[i]['quantity']*{{VAR - transactionProducts}}[i]['price']*{{VAR - Datalayer - Pricefactor Category LookupTable}}  
};   
return sum;

}

I get response 16.975. so there IS a mistake womewhere. After calculating i noticed i used 0.5 as a factor for all calculations as shown below;

2*1.99*0.5 + 3*9.99*0.5 = 1.99 + 14.985 = 16.975

Not sure what went wrong here since my javascript knowledge isnt that extensive. My best guess is that it has to do with the variable that goes in the Lookup table. The output factor remains constant ('apparel1'-->0.5) but it has to fluctuate based on which calculation we're trying to make.

The inputfunction in the lookuptable goes like this;

function() {  
 for (var i = 0; i < {{VAR - transactionProducts}}.length; i++){    
var cat = {{VAR - transactionProducts}}[i]['category']   
return cat 
};
 } 

That's the situation.


Solution

  • function() {
    var lookupTable = {
      'Apparel1': function() {
        return 0.5;
      },
      'Apparel2': function() {
        return 0.4;
      },
      'Apparel3':  function() {
        return 0.25;
      },
      'Apparel4':  function() {
        return 2;
    }};  
       var sum = 0
          for (var i = 0; i < {{VAR - transactionProducts}}.length; i++){
     sum += {{VAR - transactionProducts}}[i]['quantity']*{{VAR - transactionProducts}}[i]['price']*
       lookupTable[{{VAR - transactionProducts}}[i]['category']]()
    };
      return sum;
    }