2017-09-27 02:33:08 +08:00
|
|
|
import { ComposerExtension } from 'nylas-exports';
|
|
|
|
import { PLUGIN_ID, PLUGIN_URL } from './open-tracking-constants';
|
2017-09-07 07:19:48 +08:00
|
|
|
|
|
|
|
export default class OpenTrackingComposerExtension extends ComposerExtension {
|
|
|
|
/**
|
|
|
|
* This inserts a placeholder image tag to serve as our open tracking
|
|
|
|
* pixel.
|
|
|
|
*
|
|
|
|
* See cloud-api/routes/open-tracking
|
|
|
|
*
|
|
|
|
* This image tag is NOT complete at this stage. It requires substantial
|
|
|
|
* post processing just before send. This happens in iso-core since
|
|
|
|
* sending can happen immediately or later in cloud-workers.
|
|
|
|
*
|
|
|
|
* See isomorphic-core tracking-utils.es6
|
|
|
|
*
|
|
|
|
* We don't add a `src` parameter here since we don't want the tracking
|
|
|
|
* pixel to prematurely load with an incorrect url.
|
|
|
|
*
|
|
|
|
* We also need to add individualized recipients to each tracking pixel
|
|
|
|
* for each message sent to each person.
|
|
|
|
*
|
|
|
|
* We finally need to remove the tracking pixel from the message that
|
|
|
|
* ends up in the users's sent folder. This ensures the sender doesn't
|
|
|
|
* trip their own open track.
|
|
|
|
*/
|
2017-09-27 02:33:08 +08:00
|
|
|
static applyTransformsForSending({ draftBodyRootNode, draft }) {
|
2017-09-07 07:19:48 +08:00
|
|
|
// grab message metadata, if any
|
|
|
|
const messageUid = draft.clientId;
|
|
|
|
const metadata = draft.metadataForPluginId(PLUGIN_ID);
|
|
|
|
if (!metadata) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert a tracking pixel <img> into the message
|
2017-09-27 02:33:08 +08:00
|
|
|
const serverUrl = `${PLUGIN_URL}/open/${draft.headerMessageId}`;
|
|
|
|
const imgFragment = document
|
|
|
|
.createRange()
|
|
|
|
.createContextualFragment(
|
|
|
|
`<img class="n1-open" width="0" height="0" style="border:0; width:0; height:0;" data-open-tracking-src="${serverUrl}">`
|
|
|
|
);
|
2017-09-07 07:19:48 +08:00
|
|
|
const beforeEl = draftBodyRootNode.querySelector('.gmail_quote');
|
|
|
|
if (beforeEl) {
|
|
|
|
beforeEl.parentNode.insertBefore(imgFragment, beforeEl);
|
|
|
|
} else {
|
|
|
|
draftBodyRootNode.appendChild(imgFragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
// save the uid info to draft metadata
|
|
|
|
metadata.uid = messageUid;
|
2017-09-26 13:35:25 +08:00
|
|
|
draft.directlyAttachMetadata(PLUGIN_ID, metadata);
|
2017-09-07 07:19:48 +08:00
|
|
|
}
|
|
|
|
|
2017-09-27 02:33:08 +08:00
|
|
|
static unapplyTransformsForSending({ draftBodyRootNode }) {
|
2017-09-07 07:19:48 +08:00
|
|
|
const imgEl = draftBodyRootNode.querySelector('.n1-open');
|
|
|
|
if (imgEl) {
|
|
|
|
imgEl.parentNode.removeChild(imgEl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|