Skip to content

Latest commit

 

History

History
248 lines (208 loc) · 6.76 KB

operator_convert.md

File metadata and controls

248 lines (208 loc) · 6.76 KB

Convert Operator

The convert operator can be used to convert values from one type to another. Values can be converted to and from the following types: bool, date, int, real, or string. If the value to be converted is an array, each element in the list will be converted to the desired type. If the array has only a single value, the property will be set to the single converted value. For example, price: ['24.99'] would become price: 24.99 if the desired conversion is to a real value.

Convert is most useful when paired with FS.event for cart and checkout events where the data layer property is a string and the value should ideally be numeric.

Options

Options with an asterisk are required. Note that enumerate can be used by itself or with properties.

Option Type Default Description
enumerate* boolean false Automatically converts all string values into their numeric equivalent.
ignore string or string[] undefined Can be paired with enumerate option to ignore one or more properties when performing the enumerate.
ignoreSuffixed boolean true Can be paired with enumerate option to ignore any properties with known type suffixes when performing the enumerate
force boolean false If the property is undefined or has value null, forcibly add the property with value 0, 0.0,false or empty string.
index number 0 Position of the object to convert in the operator input list.
preserveArray boolean false If the conversion value is a list, keep the array type even if the array has a single value.
properties* string or string[] undefined List of properties to convert.
type* string undefined The desired type to convert properties to.

Tip: Set the properties option to the string * to convert all properties in an object to a desired type.

Usage

Converting specific cart prices

Rule

{
 source: 'digitalData.cart.price',
 operators: [
   { name: 'convert', properties: 'basePrice,taxRate,shipping,priceWithTax,cartTotal', type: 'real' },
   { name: 'insert', value: 'Product Viewed' },
 ],
 destination: 'FS.event'
}

Input

[
 {
  basePrice: '15.55',
  voucherCode: '',
  voucherDiscount: 0,
  currency: 'USD',
  taxRate: '0.09',
  shipping: '5.0',
  shippingMethod: 'UPS-Ground',
  priceWithTax: '16.95',
  cartTotal: '21.95',
 }
]

Output

[
 'Product Viewed',
 {
  basePrice: 15.55,
  voucherCode: '',
  voucherDiscount: 0,
  currency: 'USD',
  taxRate: 0.09,
  shipping: 5.0,
  shippingMethod: 'UPS-Ground',
  priceWithTax: 16.95,
  cartTotal: 21.95,
 }
]

Automatically convert all "numeric" strings

Rule

{
 source: 'digitalData.cart.price[(available,basePrice,cartTotal,priceWithTax)]',
 operators: [
   { name: 'convert', enumerate: true },
   { name: 'insert', value: 'Product Viewed' },
 ],
 destination: 'FS.event'
}

Input

[
 {
  available: 'false',
  basePrice: '15.55',
  priceWithTax: '16.95',
  cartTotal: '21.95',
 }
]

Output

[
 'Product Viewed',
 {
  available: 'false',
  basePrice: 15.55,
  priceWithTax: 16.95,
  cartTotal: 21.95,
 }
]

Automatically convert all "numeric" strings ignoring a specific property

Rule

{
 source: 'digitalData.cart.price[(userId, available,basePrice,cartTotal,priceWithTax)]',
 operators: [
   { name: 'convert', enumerate: true, ignore: 'userId' },
   { name: 'insert', value: 'Product Viewed' },
 ],
 destination: 'FS.event'
}

Input

[
 {
  userId: '300456',
  available: 'false',
  basePrice: '15.55',
  priceWithTax: '16.95',
  cartTotal: '21.95',
 }
]

Output

[
 'Product Viewed',
 {
  userId: '300456',
  available: 'false',
  basePrice: 15.55,
  priceWithTax: 16.95,
  cartTotal: 21.95,
 }
]

Automatically convert all "numeric" strings ignoring suffixed property

(ignoreSuffixed defaults to true)

Rule

{
 source: 'digitalData.cart.price[(userId, available,basePrice,cartTotal,priceWithTax)]',
 operators: [
   { name: 'convert', enumerate: true },
   { name: 'rename', properties: { userId: 'userId_str' } },
   { name: 'insert', value: 'Product Viewed' },
 ],
 destination: 'FS.event'
}

Input

[
 {
  userId: '300456',
  available: 'false',
  basePrice: '15.55',
  priceWithTax: '16.95',
  cartTotal: '21.95',
 }
]

Output

[
 'Product Viewed',
 {
  userId_str: '300456',
  available: 'false',
  basePrice: 15.55,
  priceWithTax: 16.95,
  cartTotal: 21.95,
 }
]

Convert "numeric" strings with specific property conversions

Rule

{
 source: 'digitalData.cart.price[(basePrice,cartTotal,priceWithTax,available)]',
 operators: [
   { name: 'convert', enumerate: true, properties: 'available', type: 'bool' },
   { name: 'insert', value: 'Product Viewed' },
 ],
 destination: 'FS.event'
}

Input

[
 {
  available: 'false',
  basePrice: '15.55',
  priceWithTax: '16.95',
  cartTotal: '21.95',
 }
]

Output

[
 'Product Viewed',
 {
   available: false,
  basePrice: 15.55,
  priceWithTax: 16.95,
  cartTotal: 21.95,
 }
]