Skip to content

includesAny — GTM Variable Template for Array

VARIABLES › ARRAY
includesAny CORE Array

Checks if any of the specified terms are found in the source array or string.


When to Use This

Array Processing

Iterate, filter, map, and reshape arrays of items for batch data operations.

Type Conversion

Safely convert between data types — strings, numbers, booleans, arrays, objects.

Comparison

Test equality, containment, and ordering between values.


Examples

Term found returns true
INPUT
Source Content: The quick brown fox jumps over the lazy dog
Search Terms: ["quick", "fox"]
OUTPUT
true
Comma-separated match
INPUT
Source Content: The quick brown fox jumps over the lazy dog
Search Terms: quick,fox,dog
OUTPUT
true

GTM Configuration

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

includesAny
Source Content
💾 The value to search within.

Supported formats:
  ✓ String: "hello world"
  ✓ Array: ["a", "b", "c"]
  ✓ Comma-separated string: "a,b,c"
  ✓ JSON-stringified array or object: '["a","b"]'
Search Terms
🔍 Search terms (one or more) to find within the data.

Supported formats:
  ✓ String: "hello world"
  ✓ Array: ["a", "b", "c"]
  ✓ Comma-separated string: "a,b,c"
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.
Source Content array
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Search Terms array
includesAny()


Under the Hood

📜 View Implementation Code
/**
 * Checks if the source contains **any** of the provided search terms.
 *
 * @param {*} data.src - The data to search within (string, number, object, etc.).
 * @param {Array<string>|string} data.tms - Term, array or comma-separated string of terms to search for.
 * @param {string|Function} [data.out] - Output handler: function to transform result or string with format.
 *
 * Direct-mode specific parameters:
 * @param {Function} [data.pre] - Optional function to transform `src` before searching.
 *
 * @returns {boolean} True if any of the terms are found, false otherwise.
 *
 * @framework ggLowCodeGTMKit
 */
const includesAny = function(searchData, searchTerms) {
	if (searchData == null || searchTerms == null) {
		return false;
	}
	const searchString = searchData.toString();
	const safeSplit = value => typeof value === 'string' ? value.split(',') : value;
	searchTerms = safeSplit(searchTerms);
	return searchTerms.some(term => term != null && searchString.indexOf(term) > -1);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// includesAny - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const searchData = applyCast(data.pre, data.src);
return out(includesAny(searchData, data.tms));
// ===============================================================================
// includesAny(...) – Apply Mode
// ===============================================================================
/*
return function(searchData, searchTerms) {
  searchTerms = data.rp1 ? data.tms : searchTerms;
  return out(includesAny(searchData, searchTerms));
};
*/
🧪 View Test Scenarios (9 tests)
✅ '[example] Term found returns true'
✅ '[example] Comma-separated match'
✅ String with some matching terms - should return true
✅ With output function - should return transformed result
✅ With pre-processor function - should extract and search text
✅ Null source - should return false
✅ Null terms - should return false
✅ Number source with matching terms - should convert and return true
✅ JSON string with matching terms - should return true