-3

I used Google Tag Manager to create a custom data level variable to get the content of an ajax form. The result is in the attributes.response that looks like:

 response:"{\"current_url\":\"https:\\/\\/domainname.com\\/ +
          "manufacturer\\/category\\/model-number\\/\",\"h" +
          "tml"\":{\"cart_status_528\":\"\\n <div id=\\\"s" +
          ...
          "<a href=\\\"https:\\/\\/domainname.com\\/manufacturer"  +
          "-name\\/long-store-category-name\\/model-number-x\\/\\" +
          "\" class=\\\"ty-product-notification__product-name\\\"" +
          ">PRODUCT-NAME THAT I WANT<\\/a>\\n           " +
          ...
          "      <p><\\n more escaped html content          +  
          }"  

I am trying to extract/parse the attribute.response to retrieve the PRODUCT-NAME text. I have tried the following which matches in regexr. But, GTM keeps complaining there is an error in my javascript at the double quote symbol. What am I missing? Or is there a cleaner way to retrieve the text? Thanks

function() {
var regex = (?<=product-name(.|\n)*">)(.*)(?=<\\\\\/a);
var attributesResponse = {{attributes.response}};
if(regex.test{{attributesResponse}}
var ProductAddedToCart = regex.exec(attributesResponse)[1];
return ProductAddedToCart;
}
return false;
}

1 Answers1

-1

First of all, please read the top answer here: RegEx match open tags except XHTML self-contained tags

Secondly, your JS has many problems. Even the SO code highlighter indicates it. See some examples of how regex is used in JS.

The proper way to solve your task, however, would be adding a dataLayer push with the proper response details neatly stored in a dataLayer object. You would normally ask your front-end developers to add a push in their response callback. It should be trivial for them to tackle. You can read more on DL here.

BNazaruk
  • 6,300
  • 3
  • 19
  • 33