Custom Service Worker Location

Upload the service worker to a custom location.

While extremely lightweight, the aimtell-worker.js file you need to upload is rather instrumental in enabling the logic behind our platform and web push notifications in general. In short, it enables us to listen for incoming push notifications even when someone has left your website.

The need for the file to be in the root directory actually stems from the fact that the file must be in the same directory as the page where someone wants to subscribe to notifications (a security measure enforced by Google).

That said, while the file itself is required, you can place out outside of the root directory and still allow visitors to subscribe while on any page. It requires a couple of extra technical steps but if you have a developer handy they should be able to implement it without any hassle.

Note: While you can upload the aimtell-worker.js to any path on your site with this technique, it must still be on your site. Meaning, if your site is https://yoursite.com, the worker can not be referenced on https://anothersite.com

Here's a guide on how that can be done:

1. Update the worker file to pass a custom header: "Service-Worker-Allowed: /"

For this example, let's imagine you uploaded your worker to https://yoursite.com/directory/path/aimtell-worker.js.

The first step (and the most complicated) is to modify the response headers to pass back "Service-Worker-Allowed: /". There are various ways to do this.

For instance, if you are using PHP you could create a file called updated-aimtell-worker.js.php with the following:

<?php

header('Service-Worker-Allowed: /');
header('Content-Type: application/javascript');

?>

importScripts('https://cdn.aimtell.com/sdk/aimtell-worker-sdk.js');

2. Reference the newly created file in your tracking code snippet.

After creating/update the above file, your aimtell-worker should be available to be reached. So if you added it under https://yoursite.com/directory/path/aimtell-worker.js, then you should be able to visit that page and see the aimtell worker code.

2020-05-26_15-15-09

Assuming you see the file, you now need to just declare it in your tracking script. Grab your Aimtell Tracking Code from the dashboard and simply declare it using:

_at.worker = '/directory/path/aimtell-worker.js';

Specifically, it would look like:

<script>

var _at = _at || {};
_at.domain = 'website.com';
_at.owner = '76c1231hffb19e';
_at.idSite = '2634';
_at.webpushid = 'web.16.aimtell.com';
_at.worker = '/directory/path/aimtell-worker.js';
_at.attributes = {};

(function() {

var u='//s3.amazonaws.com/cdn.aimtell.com/trackpush/';
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'trackpush.min.js'; s.parentNode.insertBefore(g,s);

})();

</script>

That's it! Now you should be able to view your site and get the optin prompt as expected.