Get answers quicker by searching

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

Using Google Tag Manager and UTM
  • 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.

  • 11 Comments
  • Vote Up0Vote Down espenespen
    Posts: 11Sendy user

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

  • How would I do that?

  • Vote Up0Vote Down workhorseworkhorse
    Posts: 5Sendy user

    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

  • Vote Up0Vote Down workhorseworkhorse
    Posts: 5Sendy user

    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?

  • I think this answers that question: https://wesbos.com/mailchimp-to-sendy

  • Vote Up0Vote Down workhorseworkhorse
    Posts: 5Sendy user

    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.
All Discussions