Search code examples
google-analyticsgoogle-tag-manager

How script custom HTML tag in google tag manager with an array of events


How can create a custom HTML tag with an array of events ? The below gives me a parse error on the event array line, thank you for your help

<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
window.dataLayer.push({
"event" : {"cf7submission-trigger1","cf7submission-trigger2","cf7submission-trigger3"},
"formId" : event.detail.contactFormId,
"response" : event.detail.inputs
})
}); 
</script>

Solution

  • What you defined is not an array, this is an array:

    ["cf7submission-trigger1","cf7submission-trigger2","cf7submission-trigger3"]
    

    Anyway you can't use array as event value, but you can loop the array and send the 3 events:

    var arr = ["cf7submission-trigger1","cf7submission-trigger2","cf7submission-trigger3"];
    
    for (var i=0;i<arr.length;i++) {
      window.dataLayer.push({
        "event" : arr[i],
        "formId" : event.detail.contactFormId,
        "response" : event.detail.inputs
      })
    }