Skip to content

Disabling Peer5 Programmatically

There might be cases where you would like to disable Peer5 only on some pages and according to different params. Relevant options might be: - A / B Testing - Load Reduce - Users limitations/requests

In this guide we will walk you through the options available.

Two options regarding how to decide to disable

Disable Peer5 randomly

By using Math.random you can disable Peer5:

1
2
3
4
5
6
7
8
9
<script>
if (Math.random() > 0.5) { // 50% chance
    // Add a label to your conviva/npaw/mux that peer5 is off
    window.peer5 && peer5.configure({p2p: false});
}
else {
    // Add a label to your conviva/npaw/mux that peer5 is on
}
</script>

Disable Peer5 based on your server decision

Perform a request from your script to your server, return a response that can be true / false and disable accordingly.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<script>
    const xhr = new XMLHttpRequest();
    xhr.open("GET", 'https://example.com/peer5_status');
    xhr.onloadend = function () {
        if (xhr.responseText === 'disable') {
            // Add a label to your conviva/npaw/mux that peer5 is off
            window.peer5 && peer5.configure({p2p: false});
        }
        else {
            // Add a label to your conviva/npaw/mux that peer5 is on
        }
    }
    xhr.send();
</script>
  • You can call peer5.isEnabled() to check if Peer5 is enabled/disabled - maybe it is already disabled and you don't need to perform your logic.

Loading Peer5 using script tag vs loading dynamically

  • If Peer5 is loaded via script tag as in:
1
 <script src="//api.peer5.com/peer5.js?id=PEER5_CUSTOMER_ID"></script>

Then the logic should be done after the script tag.

  • You can implement your logic and according to that load Peer5 dynamically:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function loadScript(script, cb) {
  var scripts = Array.isArray(script) ? script.slice() : [script];
  var scriptUrl = scripts.shift();
  if (!scriptUrl) return cb();

  var scriptTag = document.createElement('script');
  scriptTag.src = scriptUrl;
  scriptTag.onload = function onLoad() {
      loadScript(scripts, cb);
  };
  scriptTag.onerror = function onError() {
      cb(true);
  };
  (document.head || document.body).appendChild(scriptTag);
}

var scriptsList = [
  "//api.peer5.com/peer5.js?id=PEER5_CUSTOMER_ID", // use your customer id
  "//api.peer5.com/peer5.clappr.plugin.js"         // replace with the relevant player plugin
];

function onScriptsLoaded() {
    var player = new Clappr.Player({
        source: 'MANIFEST_FILE',
        parentId: '#player'
    });
}

var isPeer5Session = Math.random() < 0.5;
// Add a label to your conviva/npaw/mux that peer5 is on/off
thirdPartyAnalytics.setTag(isPeer5Session ? 'peer5-on' : 'peer5-off');

if (isPeer5Session) {
  loadScript(scriptsList, onScriptsLoaded);
}
else {
  onScriptsLoaded();
}

You can adjust the deployment percent using our deployment page dashboard. Tracking a users' peer5 status can be done as follows:

1
2
// Add a label to your conviva/npaw/mux that peer5 is on/off
thirdPartyAnalytics.setTag(window.peer5 && window.peer5.isEnabled() ? 'peer5-on' : 'peer5-off');