-1

I need to capture text from the file saved under URL (ex. https://fiddle.jshell.net/robots.txt) And return it under the function.

What is on my mind:

function(){
var url = 'https://fiddle.jshell.net/robots.txt'
var storedText;

fetch(url)
  .then(function(response) {
    response.text().then(function(text) {
      storedText = text;
      done();
    });
  });
function done() {
  return storedText;
}
});

Unfortunately this function is not working. I received an error: Error at line 2, character 2: Parse error. primary expression expected Type: JavaScript compiler error

This should be used as a Custom JavasSript Variable in Google Tag Manager

Also this How do I return the response from an asynchronous call? is not explaining my issue and all solutions were checked by me, nothing worked due Parse error or console error.

I have to use GTM custom JavaScrip Variable, I can use anything else. Simpler JS than better.

Karol
  • 1
  • 2

2 Answers2

0

Function done() returns storedText value to a point of call, i. e. it works but returns nothing.

0

I would not recommend using an external call within a custom Custom JavasSript Variable in GTM:

You can not really control how often a Custom JavasSript Variable is executed in GTM. An it is executed a lot. Just do the test and run a console log from a custom javascript variable.

nikelone
  • 121
  • 3