Create a gist now

Instantly share code, notes, and snippets.

@joshfg /remote.html Secret
Last active Nov 15, 2017

What would you like to do?
Permutive for AMP - remote.html implementation
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="robots" content="noindex">
<script>
(function() {
var v = location.search.substr(1);
if (!(/^\d+(-canary)?$/.test(v))) return;
var u = 'https://3p.ampproject.net/'+encodeURIComponent(v)+'/f.js';
document.write('<script'+' src="'+encodeURI(u)+'"><'+'/script>');
})();
</script>
<script type="text/javascript">!function(t,e){e||(e=e||{},window.permutive=e,e.q=[],version="1.0",e.init=function(n,r,i){e.config={projectId:n,apiKey:r,environment:i||"production"};for(var o=["addon","events","identify","track","trigger","query","ready","segment","segments"],a=0;a<o.length;a++){var c=o[a];e[c]=function(t){return function(){var n=Array.prototype.slice.call(arguments,0);e.q.push({functionName:t,arguments:n})}}(c)}var p=t.createElement("script");p.type="text/javascript",p.async=!0;var s="https:"==t.location.protocol?"https://cdn.permutive.com":"http://cdn.permutive.com";"development"===i&&(s="https://storage.googleapis.com/permutive-sdks"),p.src=s+"/"+n+"-"+version+"-amp.js";var u=t.getElementsByTagName("script")[0];u.parentNode.insertBefore(p,u)})}(document,window.permutive);
function setTargeting(config, done) {
if (typeof config.targeting === 'object') {
config.targeting.permutive = window.context.master.psegs || '';
} else {
config.targeting = { "permutive": window.context.master.psegs || '' };
}
done(config);
}
function processPending() {
var i;
var pending = window.context.master.pending;
var len = pending.length;
for (i = 0; i < len; i++) {
pending.pop()();
}
}
</script>
</head>
<body style="margin:0">
<div id="c" style="position:absolute;top:0;left:0;bottom:0;right:0;">
<script>
draw3p(function(config, done) {
if (typeof window.context.master.pending === 'undefined') {
window.context.master.pending = [];
}
if (window.context && window.context.isMaster) {
permutive.init("PROJECT_ID","API_KEY"),permutive.segments(function(e){window.context.master.psegs=e.join(","),processPending()},"dfp"),window.setTimeout(function(){if("undefined"==typeof window.context.master.psegs){var e=function(){var e=function(e){var n=new RegExp(encodeURIComponent(e)+"=([^;]+)"),o=n.exec(document.cookie);return null!==o?decodeURIComponent(o[1]):null}("_pdfps"),n=[];return e&&(n=JSON.parse(e)),n}();window.context.master.psegs=e.join(","),processPending()}},2e3);
}
if (config.type === 'doubleclick') {
window.context.master.pending.push(setTargeting.bind(null, config, done));
} else {
done(config);
}
});
</script>
</div>
<script>if (window.docEndCallback) window.docEndCallback()</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment