project Websites / Privacy Sensitive Analytics avatar

websites/privacy-sensitive-analytics#23: Fire event on video playback



Issue Information

Issue Type: issue
Status: closed
Reported By: btasker
Assigned To: btasker

Milestone: vnext
Created: 07-Dec-24 10:06



Description

In websites/BEN#30 I'm working on replacing my javascript video embeds with standard <video> tags, allowing them to display in RSS feed readers etc.

However, the JS embed pings analytics to note that a video has been played, so that I can see what's doing well:

function BensVidgatherInfo(vid, state){
    var tz = new Date().getTimezoneOffset();

    return {
        domain : "videos.bentasker.co.uk",
        page: "/" + vid.replace(window.BensPD,""),
        referrer: document.location.href,
        referrer_domain: document.location.hostname,
        platform: navigator.platform,
        timezone: tz,
        sess_id: getSessionId(),
        state: state,
        responseTime: Date.now() - window.performance.timing.navigationStart
    }
}

function submit(info){
    var xml = new XMLHttpRequest();
    xml.onreadystatechange = function () {}; // Do nothing

    // If we're being viewed on a .onion, use the .onion instead
    // (keeps everything within the tor network)
    var tld = window.location.hostname.toLowerCase();
    if (tld.endsWith(".onion")){
        xml.open("POST", window.analytics_endpoint_onion + "/write");
    } else if (tld.endsWith(".i2p")) {
        xml.open("POST", window.analytics_endpoint_i2p + "/write");
    } else {
        xml.open("POST", window.analytics_endpoint);
    }
    xml.setRequestHeader("Content-Type", "application/json");
    xml.send(JSON.stringify(info));
}

There are two JS events which get logged

  • PlayerReady (i.e. initial load time)
  • playing (i.e. the user has opted to watch the video)

I'd like to retain that visibility.

The aim of this ticket is to have PFAnalytics add an event hook to video objects to fire when they're played. Failure to fire obviously should not block playback



Toggle State Changes

Activity


assigned to @btasker

Based on the original implementation, the play counter looks something like this

/* Build the analytics payload */
function logVideoState(video, state){

    var src = new URL(video.currentSrc);
    var tz = new Date().getTimezoneOffset();

    var p = {
        domain : src.hostname,
        page: src.pathname,
        referrer: document.location.href,
        referrer_domain: document.location.hostname,
        platform: navigator.platform,
        timezone: tz,
        sess_id: getSessionId(),
        state: state,
        responseTime: Date.now() - window.performance.timing.navigationStart    
    }
    console.log(p);
}


videos = document.getElementsByTagName('video');
for (var i=0; i < videos.length; i++){
    videos[i].addEventListener('playing', function(e){
        console.log(e);
        logVideoState(e.srcElement, '"playing"');
    });
}

I don't know if it's actually worth adding a hook for PlayReady. The analytics hook should be the last thing to run on most pages, at which point the player should already be ready - it made sense when the embed was being triggered by JS, because the start was also under our control.

I guess, though, that the whole point it to catch times when that's not true - if it's not loaded by the time analytics fires, there's almost certainly an issue.

OK, I'll add it and we'll see how it goes. The next step is to transform this into the format used by the agent - we shouldn't actually need to construct an object in the way we have above.

verified

mentioned in commit 6299340ab773e0852848ed37720be728c5fb3b23

Commit: 6299340ab773e0852848ed37720be728c5fb3b23 
Author: B Tasker                            
                            
Date: 2024-12-07T10:33:49.000+00:00 

Message

feat: add hook to video tags (websites/privacy-sensitive-analytics#23)

+32 -0 (32 lines changed)

I know why OnPlayerReady seemed odd to me now - it's VideoJS specific. We actually want the canplay event.

I think, whilst we're at it, we may want to hook the events indicating playback issues:

  • abort
  • error
  • stalled
verified

mentioned in commit c5f63d24287b3acf5c774b319584b36232155ded

Commit: c5f63d24287b3acf5c774b319584b36232155ded 
Author: B Tasker                            
                            
Date: 2024-12-07T10:37:57.000+00:00 

Message

feat: fire event when video is ready to play (websites/privacy-sensitive-analytics#23)

+7 -0 (7 lines changed)
verified

mentioned in commit d8cd475cfc14a0d85335b42695715fc03da601d0

Commit: d8cd475cfc14a0d85335b42695715fc03da601d0 
Author: B Tasker                            
                            
Date: 2024-12-07T10:40:20.000+00:00 

Message

feat: fire event if video playback breaks (websites/privacy-sensitive-analytics#23)

+15 -1 (16 lines changed)

Have just tested, as expected, canplay didn't fire because the video had loaded before analytics, but this is fine.

mentioned in issue BEN#30