Select your language

The following example script asynchronously requests the USD/EUR exchange rate from the Yahoo API in JSON format by using AJAX. As soon as the currency exchange rate data has been received, a GTM Event called "exchangeRate" is fired, sending the currency rate information along as "exchangeRateInfo" parameter in the data layer.

<script>
  
    var yahooUrl = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20csv%20where%20url%3D%22http%3A%2F%2Ffinance.yahoo.com%2Fd%2Fquotes.csv%3Fe%3D.csv%26f%3Dnl1d1t1%26s%3Dusdeur%3DX%22%3B&format=json';

    var myXMLHttpRequest = null;

    if(window.XMLHttpRequest){
      myXMLHttpRequest = new XMLHttpRequest();
    }
    else if(window.ActiveXObject){
      try{
        myXMLHttpRequest = new ActiveXObject("Msxml2.XMLHTTP.6.0");
      } catch(e){
        try{
          myXMLHttpRequest = new ActiveXObject("Msxml2.XMLHTTP.3.0");
        }
        catch(e){}
      }
    }

    myXMLHttpRequest.open('GET', yahooUrl, true);
    myXMLHttpRequest.onreadystatechange = function(){    
      if(this.readyState == 4){
        if(this.status == 200){
         dataLayer.push({'event':'exchangeRate', 'exchangeRateInfo':JSON.parse(this.responseText)});
        }
      }
    }    
    myXMLHttpRequest.send();

  </script>