Skip to content

applyFromObject — GTM Variable Template for Object

VARIABLES › OBJECT
applyFromObject CORE Object

Applies a function using values extracted from an object as parameters.


When to Use This

Object Operations

Access, merge, pick, and transform object properties and structures.

GA4 Ecommerce

Build and transform ecommerce data structures for GA4 event tracking.

Extraction

Pull specific values, segments, or patterns from complex data structures.


Examples

Multiply two properties
INPUT
multiplyApplyMode: function(a, b) { return a * b; }
multiplyFn: function() { return multiplyApplyMode; }; // simulates apply mode
const mockData = {
fn:
multiplyFn(), // fn is the returned apply-mode function
pr1: 'price',
pr2: 'quantity'
}
Apply-Mode Function:
multiplyFn(), // fn is the returned apply-mode function
First Property: price
Second Property (optional): quantity
OUTPUT
30
Null returns undefined
INPUT
Apply-Mode Function: function(a, b) { return a * b; }
First Property: price
Second Property (optional): quantity
OUTPUT
undefined

GTM Configuration

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

applyFromObject
Source Object
💾 The object to extract properties from.

Supported formats:
  ✓ Object variable: {{myObject}}
  ✓ Object literal: {price: 10, quantity: 3}
Apply-Mode Function
💾 The apply-mode function to call with the extracted property values.

Supported formats:
  ✓ Math: {{multiply(...)}}, {{divide(...)}}, {{subtract(...)}}
  ✓ Any apply-mode variable: {{myVariable(...)}}
First Property
💾 The first property name to extract from the object and pass as the first argument.

Supported formats:
  ✓ Property name: "price", "quantity"
Second Property (optional)
💾 The second property name to extract from the object and pass as the second argument.

Supported formats:
  ✓ Property name: "cost", "discount"
Third Property (optional)
💾 The third property name to extract from the object and pass as the third argument.

Supported formats:
  ✓ Property name: "minScore", "maxScore"
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the source object before extracting properties.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result before returning it (e.g., x => Math.round(x * 100) to convert margin to percentage).
Source Object string
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Apply-Mode Function string
First Property string
Second Property (optional) string
Third Property (optional) string
applyFromObject()


Under the Hood

📜 View Implementation Code
/**
 * Extracts specified properties from an object and passes them as arguments to an apply-mode function.
 * Useful for chaining with itemSetProperty to compute new properties from existing item properties.
 *
 * @param {string} data.pr1 - First property name to extract and pass as first argument.
 * @param {string} [data.pr2] - Second property name to extract and pass as second argument.
 * @param {string} [data.pr3] - Third property name to extract and pass as third argument.
 * @param {Function} data.fn - The apply-mode function to call with extracted property values.
 * @param {Function|string} [data.out] - Optional output handler.
 *
 * @returns {Function} A function that receives an object and returns the result of fn(obj[pr1], obj[pr2], obj[pr3]).
 *
 * @framework ggLowCodeGTMKit
 */
const applyFromObject = function(obj, fn, pr1, pr2, pr3) {
    if (obj == null || typeof obj !== 'object') return undefined;
    if (typeof fn !== 'function') return undefined;
    
    const arg1 = pr1 ? obj[pr1] : undefined;
    const arg2 = pr2 ? obj[pr2] : undefined;
    const arg3 = pr3 ? obj[pr3] : undefined;
    
    if (pr3) return fn(arg1, arg2, arg3);
    if (pr2) return fn(arg1, arg2);
    return fn(arg1);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// applyFromObject - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(applyFromObject(value, data.fn, data.pr1, data.pr2, data.pr3));
// ===============================================================================
// applyFromObject(...) – Apply Mode
// ===============================================================================
/*
return function(obj) {
    return out(applyFromObject(obj, data.fn, data.pr1, data.pr2, data.pr3));
};
*/
🧪 View Test Scenarios (4 tests)
✅ '[example] Multiply two properties'
✅ Two properties subtraction
✅ '[example] Null returns undefined'
✅ Invalid fn returns undefined