indexOf — GTM Variable Template for Array
indexOf EXTENDED Array
Returns the index of the first occurrence of a value in a string or array.
Examples
Find index in string
INPUT
String or Array: hello world
Search Element: o
▶️ From Index: 0
Search Element: o
▶️ From Index: 0
OUTPUT
4
Not found returns -1
INPUT
String or Array: hello world
Search Element: z
▶️ From Index: 0
Search Element: z
▶️ From Index: 0
OUTPUT
-1
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
indexOf
String or Array
💾 The string or array to search in.
Supported formats:
✓ String
✓ Array
Supported formats:
✓ String
✓ Array
Search Element
💾 The element to search for in the string or array.
Supported formats:
✓ Any
Supported formats:
✓ Any
Advanced Setting
▶️ From Index
💾 The position at which to start the search (default is 0).
Supported formats:
✓ Number
✓ String
Supported formats:
✓ Number
✓ String
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the input before internal logic (e.g., convert object to string, normalize case). Internal transformations such as case handling will still apply afterward.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result before returning it (e.g., str => str + ' €', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
String or Array array
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Search Element any
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
indexOf()
Related Variables
Same category: Array
Under the Hood
📜 View Implementation Code
/**
* Returns the index of the first occurrence of a specified value in a string or an array.
*
* @param {string|Array} data.src - The string or array to search.
* @param {any} data.elm - The element to search for in the string or array.
* @param {number|string} data.frm - The position at which to start the search (default is 0).
* @param {Function|string} [data.out] - Optional output handler: function to transform result or string with format.
*
* Direct-mode specific parameters:
* @param {Function} [data.pre] - Optional pre-processor function to transform src before searching.
*
* @returns {number} The index of the first occurrence of the search element, or -1 if not found.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const makeNumber = require('makeNumber');
const indexOf = function(input, searchElement, fromIndex) {
const fromIdx = fromIndex !== undefined ? makeNumber(fromIndex) : 0;
if (!(getType(input) === 'array' || getType(input) === 'string') ||
typeof fromIdx !== 'number' ||
fromIdx < 0) {
return -1;
}
return input.indexOf(searchElement, fromIdx);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// indexOf - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(indexOf(value, data.elm, data.frm));
// ===============================================================================
// indexOf(...) – Apply Mode
// ===============================================================================
/*
return function(value, searchElement, fromIndex) {
searchElement = data.rp1 ? data.elm : searchElement;
fromIndex = data.rp2 ? data.frm : fromIndex;
return out(indexOf(value, searchElement, fromIndex));
};
*/🧪 View Test Scenarios (9 tests)
✅ '[example] Find index in string'
✅ '[example] Not found returns -1'
✅ String with fromIndex specified - should search from that position
✅ Array with matching element - should return index
✅ Array with element not found - should return -1
✅ Array with fromIndex specified - should search from that position
✅ String with fromIndex as string - should convert and use it
✅ Array searching from middle position - should skip earlier occurrences
✅ fromIndex beyond array length - should return -1