Skip to content

Instantly share code, notes, and snippets.

@derrick-branch
Last active May 18, 2017 01:42
Show Gist options
  • Save derrick-branch/f9b1e72e506f79628ab9127dd114dd83 to your computer and use it in GitHub Desktop.
Save derrick-branch/f9b1e72e506f79628ab9127dd114dd83 to your computer and use it in GitHub Desktop.

this assumes you have node and npm installed

  1. put branch-sdk.js and sendgrid-demo.js in the same directory
  2. run node sendgrid-demo.js in that directory to examine the sample output

Note: for actual usage, be sure to use your app's branch_base_url, rather than https://vza3.app.link/3p?%243p=st as provided in the example.

module.exports = function(original_url, branch_base_url) {
if (!original_url) { return new Error('Missing original_url'); }
if (typeof original_url != 'string') { return new Error('Invalid original_url'); }
if (!branch_base_url) { return new Error('Missing branch_base_url, should be similar to https://bnc.lt/abcd/3p?%243p=xx'); }
if (typeof branch_base_url != 'string') { return new Error('Invalid branch_base_url'); }
return branch_base_url + '&%24original_url=' + encodeURIComponent(original_url);
};
// Javascript SendGrid Demo -- `node sendgrid-demo.js`
var branchSDK = require('./branch-sdk'),
BRANCH_BASE_URL = 'https://vza3.app.link/3p?%243p=st'; // replace this with the BRANCH_BASE_URL provided by Branch
var body = '<html><body><a id="some-id" class="some-class" href="http://example.com/?foo=bar">Destined to be a clickable deep link</a><a id="some-id" class="some-class" href="http://example.com/?2">mix it up</a></body></html>';
body = body.replace(/(<a\s(:?.|\s)*?href=")(.+?)(")/gi, function(v1, v2, v3, v4, v5) {
return v2 + branchSDK(v4, BRANCH_BASE_URL) + v5;
})
console.log('new body ready to be sent via email:\n', body);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment