Skip to content

Instantly share code, notes, and snippets.

@ripter
Last active January 22, 2018 19:26
Show Gist options
  • Save ripter/5b1155735a6da83fcd459705dc9fdc56 to your computer and use it in GitHub Desktop.
Save ripter/5b1155735a6da83fcd459705dc9fdc56 to your computer and use it in GitHub Desktop.
Tout Embed Codes v3
<!-- Embed Code, v1 Power User Style
No wrapper means no extra network call.
Everything is loaded in parallel.
-->
<link rel="stylesheet" href="https://tesla.tout.com/sdk/v2/sdk.css" />
<script src="https://tesla.tout.com/sdk/v2/sdk.js" async defer></script>
<script src="https://tesla.tout.com/sdk/v2/af660e.js" async defer></script>
<!-- Embed Code, v1 style
Simple config of the TOUT global.
Loads an embed script that loads the CSS/JS/Data.
-->
<script>
window.TOUT = {
brandID: 'af660e',
};
</script>
<script src="https://tesla.tout.com/sdk_embed.js" async defer></script>
// Wrap in a IIFE so we don't leak globals
void function() {
/**
* Check for valid config, abort if something is wrong.
* sdkHost is optional and by default points to production.
*/
if (!window.TOUT) {
throw new Error('window.TOUT was not found. Please check the embed code for configuration.');
}
// the name for brandID has changed over the years, so we still need to check for the older names.
const brandID = ['brandID', 'brandUid', 'branduid', 'propertyUid', 'property', 'BRAND_ID'].reduce(getValue, null);
if (!brandID) { throw new Error('TOUT Configuration Error: No brandID could be found on window.TOUT'); }
// the name for the sdkHost has also changed over the years.
const sdkHost = ['sdkHost', 'sdkhost', 'sdk_host', 'SDK_HOST'].reduce(getValue, 'platform.tout.com');
// Load the rest of TOUT SDK
const baseURL = '//' + sdkHost;
dom('link', {href: baseURL + '/sdk/v2/sdk.css', rel: 'stylesheet'});
dom('script', {src: baseURL + '/sdk/v2/sdk.js', async: true, defer: true});
// The data can be fetched, or loaded inline.
// dom('script', {src: baseURL + '/sdk/v2/' + brandID + '.js', async: true, defer: true});
// helper function to add elements to the <head>
function dom(tag, attributes) {
const elm = document.createElement(tag);
Object.keys(attributes).forEach((key) => {
elm.setAttribute(key, attributes[key]);
});
document.head.appendChild(elm);
}
// helper function to get the value
function getValue(acc, key) {
if (window.TOUT[key]) { return window.TOUT[key]; }
return acc;
}
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment