Skip to content

stringToPath β€” GTM Variable Template for URL

VARIABLES β€Ί URL
stringToPath EXTENDED URL
Direct (.tpl) Apply (.tpl)

Converts a dot/bracket notation string into a path array.



Dot notation to path
INPUT
Path to Property: user.name.first
OUTPUT
["user", "name", "first"]
Bracket notation with index
INPUT
Path to Property: items[0]
OUTPUT
["items", "0"]

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

stringToPath
Path to Property
πŸ’Ύ Property path using dot and bracket notation.
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.
Path to Property string
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
stringToPath()


πŸ“œ View Implementation Code
/**
* Converts a string to a property path array.
*
* @param {string} data.src - The string to convert to a property path array.
* @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 conversion.
*
* @returns {Array} Returns the property path array, or empty array if input is invalid.
* @author Lodash (https://lodash.com) - Original regex and parsing approach
* @see https://github.com/lodash/lodash/blob/master/stringToPath.js
* @modified by GwenG – Rewrote the full function to adapt to the ggLowCodeGTMKit framework
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const stringToPath = function(path) {
if (getType(path) === 'array') return path;
if (typeof path !== 'string') return [];
if (path === '') return [];
if (path.indexOf('[') === -1 && path.indexOf('\\') === -1) return path.split('.');
const pathSegmentRegex = "[^.[\\]]+|\\[(?:(-?\\d+(?:\\.\\d+)?)|([\"'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2)\\]";
const escapedCharRegex = '\\\\(.)';
let pathSegments = [];
let unparsed = path;
while (unparsed.charAt(0) === '.') {
pathSegments.push('');
unparsed = unparsed.slice(1);
}
while (unparsed.length > 0) {
const segmentMatch = unparsed.match(pathSegmentRegex);
if (!segmentMatch) {
break;
}
const matchedSegment = segmentMatch[0];
const numberIndex = segmentMatch[1];
const quoteType = segmentMatch[2];
const quotedKey = segmentMatch[3];
if (quoteType && quotedKey) {
let escapedQuotedKey = quotedKey;
let unescapedKey = '';
while (escapedQuotedKey.length > 0) {
const escapeMatch = escapedQuotedKey.match(escapedCharRegex);
if (!escapeMatch) {
unescapedKey += escapedQuotedKey;
break;
}
unescapedKey += escapedQuotedKey.substring(0, escapeMatch.index);
unescapedKey += escapeMatch[1];
escapedQuotedKey = escapedQuotedKey.substring(escapeMatch.index + escapeMatch[0].length);
}
pathSegments.push(unescapedKey);
} else if (numberIndex) {
const stringifiedIndex = numberIndex.toString();
pathSegments.push(stringifiedIndex);
} else if (matchedSegment) {
pathSegments.push(matchedSegment);
}
unparsed = unparsed.substring(segmentMatch.index + matchedSegment.length);
}
let trailingDots = 0;
let dotPosition = path.length - 1;
while (dotPosition >= 0 && path.charAt(dotPosition) === '.') {
trailingDots++;
dotPosition--;
}
for (let dotIndex = 0; dotIndex < trailingDots; dotIndex++) {
pathSegments.push('');
}
return pathSegments;
};
const s
πŸ§ͺ View Test Scenarios (23 tests)
βœ… '[example] Dot notation to path'
βœ… '[example] Bracket notation with index'
βœ… Mixed dot and bracket notation with numbers
βœ… Bracket notation with quoted strings containing dots
βœ… Path with leading dot
βœ… Multiple consecutive bracket notations
βœ… Path with consecutive dots (skips empty parts)
βœ… Path with unclosed bracket
βœ… Complex path with multiple notation types
βœ… Path with NaN inside brackets
βœ… Empty brackets between segments
βœ… Extremely complex mixed path
βœ… Multiple leading dots
βœ… Escaped quotes in bracket notation
βœ… Brackets inside quotes
βœ… Floating point numbers in brackets
βœ… Negative numbers in brackets
βœ… Mixed quote types
βœ… Just a string with no dots or brackets
βœ… Empty string
βœ… String with just a dot
βœ… Trailing dots
βœ… Invalid input return empty array