Last active
June 23, 2019 01:30
-
-
Save cssimsek/cd151ba2fa6a9346db7280d731333d28 to your computer and use it in GitHub Desktop.
Flatten W3C Data Layer to flat Tealium utag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (w) { | |
let w3c_DL_NS = 'YOUR_W3C_DL_NS'; | |
if (w3c_DL_NS in w && 'utag_data' in w) { | |
w3c_DL_NS = w[w3c_DL_NS]; | |
flatten(w3c_DL_NS); | |
} | |
function flatten(object, keyTree) { | |
keyTree = keyTree || 'w3c'; | |
if (object instanceof Object) { | |
if (Array.isArray(object)) { | |
for (var i = 0; i < object.length; i += 1) { | |
flatten(object[i], keyTree.concat('-' + i)); | |
} | |
} else { | |
for (var prop in object) { | |
if (object.hasOwnProperty(prop)) { | |
flatten(object[prop], keyTree.concat('-' + prop)); | |
} | |
} | |
} | |
} else { | |
w['utag_data'][keyTree] = object; | |
} | |
} | |
})(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In Node.js
object instanceof Object
appears to fail. Replacing it with/(Array|Object)/g.test(object.constructor.toString())
offers a solution.