1

I am using the sendgrid-nodejs library to send emails using SendGrid. I want to disable click-tracking on a per-email basis.

I understand that you can include an attribute within dynamic templates to disable click tracking:

Click tracking can be turned off for individual links by including the clicktracking=off attribute inside the anchor of an HTML link before the href. For example, <a clicktracking=off href="http://example.com">link text</a> would not be tracked.

However, I wish to control this programmatically.

According to SendGrid documentation, it is possible to disable click-tracking by using the clicktrack filter:

{
  "filters": {
    "clicktrack": {
      "settings": {
        "enable": 0,
        "enable_text": false
      }
    }
  }
}

Looking at the Mail constructor, it appears we have the ability to set headers. The type bindings indicate it expects header values to be a string.

headers?: { [key: string]: string }

Note: I can confirm this per SendGrid's error return (if attempting to pass an object):

{
  "body": {
    "errors": [
      {
        "message": "Invalid type. Expected: string, given: object.",
        "field": "headers",
        "help": "http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.headers"
      }
    ]
  }
}

Regardless of what I pass, nothing seems to have any impact. The emails are being sent successfully, but click-tracking is not being disabled.

const { SENDGRID_KEY } = process.env
const mail = require('@sendgrid/mail')
mail.setApiKey(SENDGRID_KEY)

mail.send({
  headers: {
    // this doesn't have any impact
    "X-SMTPAPI": JSON.stringify({
      filters: {
        clicktrack: {
          settings: {
            enable: 0,
            enable_text: false
          }
        }
      }
    }),
    // neither does this
    "filters": JSON.stringify({
      clicktrack: {
        settings: {
          enable: 0,
          enable_text: false
        }
      }
    }),
  },
  to: 'somebody@email.com',
  from: 'nobody@email.com',
  templateId: 'd-xxxxxxxxxxxxxxxxxxxxxxxx',
  dynamic_template_data: {
    subject: 'Hello World'
  }
})

Why isn't this working?

Crayons
  • 1,906
  • 1
  • 14
  • 35

1 Answers1

3

I've found my answer. There is a trackingSettings property available:

const { SENDGRID_KEY } = process.env
const mail = require('@sendgrid/mail')
mail.setApiKey(SENDGRID_KEY)

mail.send({
  trackingSettings: {
    clickTracking: {
      enable: false,
      enableText: false
    }
  },
  to: 'somebody@email.com',
  from: 'nobody@email.com',
  templateId: 'd-xxxxxxxxxxxxxxxxxxxxxxxx',
  dynamic_template_data: {
    subject: 'Hello World'
  }
})
Crayons
  • 1,906
  • 1
  • 14
  • 35