my_first_step
my_first_step

Reputation: 33

how to track click,open mails using mailgun and nodejs?

Could anyone tell me how to track mails by using mailgun api with nodejs?

I am trying to send and track mail for my personal use. I got the below example from tutorials and working fine. Now I want to track the opened/click mails.

serv.js

app.post('/', function(req, res) {
        var api_key = 'mykey';
        var domain = 'mydomain.in';
        var Mailgun = require('mailgun-js');

        var mailgun = new Mailgun({ apiKey: api_key, domain: domain });
        var data = {
            from: "[email protected]",
            to: "[email protected]",            
            subject: req.body.subject, 
            text: req.body.plaintext,
            'o:tag': req.body.tag,
           // 'o:tracking-clicks':req.body.trackingclicks,
            //'o:tracking-opens':req.body.trackingopens
        };
        console.log(req.body);
        mailgun.messages().send(data, function(error, body) {
            console.log(body);
            //Email not sent
            if (error) {
                res.render('index', { title: 'No Email', msg: 'faild', err: true })
            }
            //Yay!! Email sent
            else {
                res.render('index', { title: success.', err: false })
            }
        });

    });

Upvotes: 1

Views: 1710

Answers (1)

rsp
rsp

Reputation: 111346

You will no be able to track mail that is opened reliably, unless you embed an external image in the email's HTML and log on the server when it is downloaded, but people may still block external resources for getting downloaded for exactly that reason that they don't want to be tracked. Spammers use such techniques to track which email accounts are worth spamming so it became an issue.

For tracking the clicking of some link or button in the email, you need to make an endpoint on your backend to track the clicks on those links and integrate it with the sending of emails - to know which link is from which email. This shouldn't be blocked by email clients because it is their explicit action of clicking that link but some people may stil choose to not click any links in emails for security reasons.

Upvotes: 2

Related Questions