Skip to content

Using Google Tag Manager and UTM

edited December 2020 in Questions

Does anyone know how to use Google Tag Manager to subscribe users to a list? I can pull the email address from the Data Layer, that's easy, I can't figure out how to post it to the Sendy API.

Thanks.

Comments

  • Sending a HTTP request using JavaScript to the form URL should work I would think.

  • How would I do that?

  • How did the email address get into the datalayer in the first place? I ask b/c it would probably be more reliable to submit it from the web server to Sendy API rather than fire a GTM tag to the Sendy API. GTM is blocked by ad blockers for example. Not only that, but doing this in GTM would expose your Sendy API key which is a security issue.

  • That's a good point but the form is a third party form on my website so I can't submit it directly but I want to capture those addresses

  • Makes sense. I was able to get this to work with something like this:

    <script>    
        var url = "https://SENDYDOMAIN/subscribe";
        var method = "POST";
        var postData = "api_key=APIKEY&email={{EMAIL DLV HERE}}&list=LIST ID";
        var shouldBeAsync = true;
        var request = new XMLHttpRequest();
        request.onload = function () {
           var status = request.status;
           var data = request.responseText;
        }   
        request.open(method, url, shouldBeAsync);
        request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        request.send(postData); 
    </script>
    

    In my tag, but you'll probably get lots of CORS errors so you'll need to add Access-Control-Allow-Origin response headers on the server where Sendy is installed to allow POST requests from the server where the form is. I'm also not a great JavaScript developer so there is probably / may be a better way than the above, it was adapted from Stack Overflow (I know, I know).

  • Thanks. Working except the CORS errors, how would I install Access-Control-Allow-Origin?

  • edited February 2021

    Great. If you have apache server, you'd edit your Sendy .htaccess file. You'd need to test, but I think you'd want:

    Header set Access-Control-Allow-Origin: https://domain-with-form.com/
    Header set Access-Control-Request-Method: POST
    

    I just tested that and it worked for me.

  • I just edited the subscribe.php with this:
    Header set Access-Control-Allow-Origin: https://domain-with-form.com/

  • This ends up creating a huge error_log file in /var/log, not sure why, I just end up deleting it every couple of days, but it ends up being like 200mb, not sure why. Any insight?

  • I think what happened was I updated Sendy and because I had only put that domain allow in the subscribe.php when I updated it my adjustment was lost, now I put it in the .htaccess and I think it's working

This discussion has been closed.