Created
July 10, 2018 21:02
-
-
Save perrygovier/7d9f730e51729da29575beffe9601ee8 to your computer and use it in GitHub Desktop.
Get the current google optimize test index from google tag manager
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 getOptimizeTestIndexFromTageManager() { | |
// returns null if not a test, 0 for the control, or 1 [2,3,4...] for | |
// the variant index | |
var optimizeContainerID = 'GTM-XXXXXXX' | |
if (!window.google_tag_manager) { | |
console.warn('Google Tag Manager object not found'); | |
return; | |
} | |
var experimentID = window.google_tag_manager[optimizeContainerID] ? | |
window.google_tag_manager[optimizeContainerID].experiment : null; | |
if ( | |
!window.google_tag_manager[optimizeContainerID] || | |
!window.google_tag_manager[optimizeContainerID].experiment | |
) { | |
return null; | |
} | |
return parseInt( | |
window.google_tag_manager[optimizeContainerID].experiment.split('$')[1], | |
10 | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment