Create a gist now

Instantly share code, notes, and snippets.

@joshfg /remote.html Secret
Last active Feb 8, 2018

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 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) {
!function(e,n,t,r,o,i){if(!n){n=n||{},window.permutive=n,n.q=[],n.config=i||{},n.config.projectId=r,n.config.apiKey=o,n.config.environment=n.config.environment||"production";for(var c=["addon","identify","track","trigger","query","segment","segments","ready","on","once","user"],a=0;a<c.length;a++){var s=c[a];n[s]=function(e){return function(){var t=Array.prototype.slice.call(arguments,0);n.q.push({functionName:e,arguments:t})}}(s)}var p=window.Worker?"async":"blocking",g=e.createElement("script");g.type="text/javascript",g.async=!0;var m=("https:"==e.location.protocol?"https://":"http://")+"cdn.permutive.com";g.src=m+"/"+r+"-"+p+"-amp.js";var u=e.getElementsByTagName(t)[0];u.parentNode.insertBefore(g,u)}}(document,window.permutive,"script",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