I need to save the email from this script:
<script>
window.renderOptIn = function() {
window.gapi.load('surveyoptin', function() {
window.gapi.surveyoptin.render(
{
"merchant_id": 0000,
"order_id": "100101205",
"email": "tester@example.com",
"delivery_country": "XX",
"estimated_delivery_date": "2021-09-03"
});
});
}
</script>
Is there a way to do that? It's physically in the code but I can't think of a way to get it.
It's e-commerce related. The script is on the "thank-you" page. I need to save the email to a variable so I can use it for google's enhanced conversion tracking.
Accessing the document.scripts and filtering on content
Regular expression or split.
const code = [...document.scripts].filter(script => script.textContent.includes('surveyoptin'))
const match = code[0].textContent.match(/(?:render\(.*\s+)({.*\s+)+/s)
const str = match[1].split(");")[0]; // forgot how to ignore trailing stuff after the group: );
console.log(str)
const obj = JSON.parse(str); // fails on 0000
console.log(obj.email)
// alternative - this is inflexible on spacing etc:
console.log(
code[0]
.textContent
.split('"email": "')[1]
.split('"')[0]
)
<script src="x.js"></script>
<script>const survey = "Survey"; // dummy script</script>
<script>
window.renderOptIn = function() {
window.gapi.load('surveyoptin', function() {
window.gapi.surveyoptin.render(
{
"merchant_id": "0000",
"order_id": "100101205",
"email": "tester@example.com",
"delivery_country": "XX",
"estimated_delivery_date": "2021-09-03"
});
});
}
</script>