使用jQuery和Google跟踪代码管理器通过SendGrid发送交易电子邮件

时间:2019-12-17 21:23:06

标签: sendgrid sendgrid-api-v3

当某些事件被触发时,我正尝试使用google标记管理器自定义html标记发送电子邮件。

这是代码(需要时省略XXXXX)

<script>
    var settings = {
      "async": true,
      "crossDomain": true,
      "url": "https://api.sendgrid.com/v3/mail/send",
      "method": "POST",
      "headers": {
        "authorization": "Bearer XXXXXXXXXXXXX",
        "content-type": "application/json"
      },
      "processData": false,
      "data": "{\"personalizations\":[{\"to\":[{\"email\":\"XXXXXXXXXX\",\"name\":\"John Doe\"}],\"dynamic_template_data\":{\"verb\":\"yes\",\"adjective\":\"test\",\"noun\":\"\",\"currentDayofWeek\":\"\"},\"subject\":\"Hello, World!\"}],\"from\":{\"email\":\"XXXXXXXXXXXXXXX\",\"name\":\"John Doe\"},\"reply_to\":{\"email\":\"XXXXXXXXXXXXXXX\",\"name\":\"John Doe\"},\"template_id\":\"XXXXXXXXXXXXXXX"}"
    }

    jQuery.ajax(settings).done(function (response) {
      console.log(response);
    });
    </script>

现在,当使用内置的发送网格“试用”功能时,运行此块(减去脚本标签)。为什么从GTM无法正常工作?

1 个答案:

答案 0 :(得分:0)

在jQuery准备就绪之前,可能会添加并运行诸如此类代码的声音。尝试对代码进行以下更新:

<script>
  jQuery(function() {
    var settings = {
      "async": true,
      "crossDomain": true,
      "url": "https://api.sendgrid.com/v3/mail/send",
      "method": "POST",
      "headers": {
        "authorization": "Bearer XXXXXXXXXXXXX",
        "content-type": "application/json"
      },
      "processData": false,
      "data": "{\"personalizations\":[{\"to\":[{\"email\":\"XXXXXXXXXX\",\"name\":\"John Doe\"}],\"dynamic_template_data\":{\"verb\":\"yes\",\"adjective\":\"test\",\"noun\":\"\",\"currentDayofWeek\":\"\"},\"subject\":\"Hello, World!\"}],\"from\":{\"email\":\"XXXXXXXXXXXXXXX\",\"name\":\"John Doe\"},\"reply_to\":{\"email\":\"XXXXXXXXXXXXXXX\",\"name\":\"John Doe\"},\"template_id\":\"XXXXXXXXXXXXXXX"}"
    }

    jQuery.ajax(settings).done(function (response) {
      console.log("SendGrid Response:", response);
    });
  });
</script>

尝试一下,看看是否在控制台中看到标有“ SendGrid Response:”的任何内容。这肯定会让我们知道该脚本是否正在运行。