Last active
June 22, 2018 18:34
-
-
Save cwparsons/597f087d6830726ef8f22027f5bf1c87 to your computer and use it in GitHub Desktop.
Send an event to multiple
This file contains hidden or 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
/** | |
* # Usage | |
* | |
* 1. On an individual tag, update the tag configuration to have a new *Field to set*. | |
* 2. Set the field name to `customTask`. | |
* 3. Create a new variable for the value using the *Custom JavaScript* variable type. | |
* 4. Use below for the custom JavaScript. | |
*/ | |
function() { | |
// console.log('Running customTask function...'); | |
// Replace newTrackingIds value with the UA property to which you want to duplicate this tag | |
var newTrackingIds = ['UA-XXX-Y', 'UA-XXX-Z']; | |
var globalSendTaskName = '_' + newTrackingIds[0] + '_originalSendTask'; | |
return function (customModel) { | |
try { | |
// console.log('Running customTask created function...'); | |
window[globalSendTaskName] = window[globalSendTaskName] || customModel.get('sendHitTask'); | |
customModel.set('sendHitTask', function (sendModel) { | |
// console.log('Running `sendHitTask`...'); | |
var hitPayload = sendModel.get('hitPayload'); | |
var trackingId = new RegExp(sendModel.get('trackingId'), 'gi'); | |
window[globalSendTaskName](sendModel); | |
for (var i = 0; i < newTrackingIds.length; i++) { | |
var newTrackingId = newTrackingIds[i]; | |
// console.log('Set hitPayload on ' + newTrackingId); | |
sendModel.set('hitPayload', hitPayload.replace(trackingId, newTrackingId), true); | |
window[globalSendTaskName](sendModel); | |
} | |
}); | |
} catch (e) { | |
console.log('Error running customTask function within Google Tag Manager.'); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment