1

I'm trying to implement signup with confirmation email with Django using SendGrid as email provider, my 'SignUpActivationView' code validates the token and then redirects the user to the home page, where a success/error message is displayed using the messages framework:

class SignUpActivationView(RedirectView):
    redirect_url = reverse_lazy('link_list')

    def dispatch(self, request, *args, **kwargs):
        ...
        # if token is valid:
        messages.error(self.request, 'Your account is now active.')
        return HttpResponseRedirect(self.redirect_url)
        # if token is invalid:
        messages.error(self.request, 'Your account could not be activated.')
        return HttpResponseRedirect(self.redirect_url)

So far my approach works as long as I copy-paste the link in my browser, if I click on it instead, the user is activated but the success message is not shown in my application. I believe this is because SendGrid's Click Tracking service is wrapping my link:

SendGrid Click Tracking Service

A workaround I found is to tell SendGrid to NOT track the link by adding and the 'clicktracking=off' HTML attribute, the disadvantage is that I can only tell SendGrid not to track links in the HTML version of my email. In case the user's email client decides to open the Plain text version of the email, then link will still be wrapped.

 # solution: do not track clicks
 # caveat: only works in HTML, not in plain text emails
 <a clicktracking=off href="http://foo.bar">Confirmation link</a>

So Link Tracking is pretty month mandatory for Plain text emails.

Is there any way to make the messages framework work with Link Tracking?

syrup
  • 985
  • 7
  • 14

1 Answers1

1

When SendGrid tracks your link, the "click" goes to SendGrid, which returns a 302 Found redirect to the URL you provided in the original email, before SendGrid tracked it.

So, as long as you provide the correct link there, by the time the "click" is getting to your server, it's back to that, with the appropriate token.
You can review this behavior in Chrome's Developer Tools, under the Network tab, which should help you troubleshoot what's happening here.

Also, you can disable all of SendGrid's Click Tracking, instead of just link-by-link, under their Settings: Tracking Settings section.
By default, SendGrid does not track plain-text links, since it makes them so much longer. You'd have to actively enable that under those same settings if you want it to do that. It's very rare that a recipient views in Plain Text these days, so it's not a significant contributor to your engagement statistics, so enabling that feature is generally not recommended.

jacobmovingfwd
  • 2,185
  • 14
  • 11
  • Thanks, I'll try to troubleshoot it, but for now I've disabled plain-text tracking (it was enabled by default in my case). There is a toggle on SendGrid's settings! – syrup Oct 27 '18 at 21:32