Skip to content

Instantly share code, notes, and snippets.

@Smudded
Last active August 22, 2019 13:34
Show Gist options
  • Save Smudded/0852a01aa9398e90565292b1d772de3d to your computer and use it in GitHub Desktop.
Save Smudded/0852a01aa9398e90565292b1d772de3d to your computer and use it in GitHub Desktop.

Embedded Install relies on the server to embed all relevant JavaScript in the page itself on first load rather than relying on a user’s browser to retrieve the scripts from a third party server. This eliminates the possibility of adblockers preventing Admiral’s JavaScript from being delivered to your adblocking users.

Admiral provides an API endpoint that returns JavaScript specific to your property. Using a server-side technology like PHP you can retrieve Admiral’s JavaScript from this API and embed it in the page before the closing body tag. This ensures that Admiral’s JavaScript does not block any of the content on the page from loading.

Keep in mind that with Embedded Install Admiral’s JavaScript is beholden to your server's cache settings. Options for products like Engage will not change when edited until the cache for each page is invalidated on your servers.

The examples provided below show how this endpoint can be utilized to retreive the measure script on the server and embed it into a page using PHP. Each product's script is retrieved from separate endpoints, so if you want to retrieve the Engage script you can replace /measure with /engage in the endpoint URL to retreive the Engage script.

<?php
$url = "http://delivery.api.getadmiral.com/script/{yourPropertyIDHere}/measure";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xff = "X-Forwarded-For: ";
if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
$xff .= $_SERVER["HTTP_X_FORWARDED_FOR"] . ", ";
}
$xff .= $_SERVER["REMOTE_ADDR"];
$headers = array($xff);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$src = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
if ($code === 200) {
// this is where it would put the script on the page
echo "<script type="text/javascript">$src</script>";
}
curl_close($ch);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment