Extracting User Engagement stats per user per video through API

Hello,
I would be interested in getting user engagement stats per user per video through APIs in Python. I have been going through the various reports that getTable method supports, but not sure which report would fit my use case. Highly appreciate guidance on where should I be looking for that kind of data. I have also provided an example for better understanding below.

Example:
User 1 :
Video1 (total_completion_rate)
Video2 (total_completion_rate)
Video3 (total_completion_rate)

User 2:
Video1 (total_completion_rate)
Video2 (total_completion_rate)
Video3 (total_completion_rate)

User3:
Video1 (total_completion_rate)
Video2 (total_completion_rate)
Video3 (total_completion_rate)

Thank you.

Hi @testKaltura21,

You can use the below PHP script as reference and write the equivalent in Python (or any other language for that matter).

This script also gets the referenceId for each entry (if set) and the last view time (if available - this relies on viewing history data being present).
If you have no use for these metrics, you may drop the code used to obtain them, of course.

<?php
if($argc < 6){
    die('Usage: '.$argv[0] .' <partner_id> <admin secret> <service url> <start UNIX timestamp> <end UNIX timestamp> </path/to/output/file>'."\n");
}

// replace with path to the PHP 5.3+ client, which can be obtained from https://github.com/kaltura/KalturaGeneratedAPIClientsPHP/archive/v19.1.0.tar.gz
require_once('/home/jesse/tmp/KalturaGeneratedAPIClientsPHP-18.15.0/KalturaClient.php');
$partnerId = $argv[1];
$secret = $argv[2];
$config = new KalturaConfiguration($partnerId);
$config->serviceUrl = $argv[3];
$from_date = $argv[4];
$to_date = $argv[5];
$output_file = $argv[6];
$client = new KalturaClient($config);
$ks = $client->generateSessionV2($secret, $argv[0], KalturaSessionType::ADMIN, $partnerId, 86400, 'disableentitlement');
$client->setKs($ks);

$report_id = 3020;
$params = "from_date=$from_date;to_date=$to_date";
$excludedFields = "";

try {
    $url = $client->report->getCsvFromStringParams($report_id, $params, $excludedFields);
    if (!preg_match('/^http/',$url)){
	$url = 'https://' . $url;
    }
    $data = file_get_contents($url);
    $data_as_array=explode("\n",$data);
    // drop header
    array_shift($data_as_array);
    $out = array();
    $out[] = array('USER ID','USER EMAIL','ENTRY ID','ENTRY NAME','TOTAL COMPLETION RATE','REFERENCE ID','LAST VIEW TIME');
    foreach ($data_as_array as $rec){
	$rec_as_array = explode(',',$rec);
	if (count($rec_as_array) > 1){
	    $user_id =  $rec_as_array[0];
	    $user_email = $rec_as_array[1];
	    $entry_id = $rec_as_array[2];
	    // remove quotes. fputcsv() will add them where they are needed
	    $entry_name = trim($rec_as_array[3],'"');
	    $total_completion_rate = $rec_as_array[5];

	    if (isset($user_id)){
		try {
		    $entry = $client->media->get($entry_id);
		    $reference_id = $entry->referenceId;
		    $filter = new KalturaUserEntryFilter();
		    $filter->entryIdEqual = $entry_id;
		    $filter->userIdEqual = $user_id;
		    $pager = new KalturaFilterPager();
		    $user_entry = $client->userEntry->listAction($filter, $pager);
		    if ($user_entry->objects){
			$last_view_time = date('d-m-Y', $user_entry->objects[0]->updatedAt);
		    }else{
			$last_view_time = 'N/A';
		    }

		    // for debugging purposes
		    //echo $user_id . ',' . $user_email . ',' . $entry_id . ',' . $entry_name . ',' . $total_completion_rate . ',' . $reference_id . ',' . $last_view_time . "\n";  

		    $out[]=array($user_id,$user_email,$entry_id,$entry_name,$total_completion_rate,$reference_id,$last_view_time);

		} catch (Exception $e) {
		    //echo $e->getMessage() . "\n";
		}
	    }
	}
    }

    $fp = fopen($output_file, 'w');
    foreach ($out as $rows) {
	fputcsv($fp, $rows);
    }
    fclose($fp);
} catch (Exception $e) {
    echo $e->getMessage();
}

?>

Hello @jess,
Thank you for your response. I tried using the details(report_id, params, excludedFields) that you provided in the developer console and this is the response that I get. I can only see the headers but not the data pertaining to it. I have attached a screenshot for reference. Am I missing something?

Hi @testKaltura21 ,

As you can see in the usage message, the dates should be specified as UNIX timestamps (aka Epoch):

That works! Thank you for that information. Is the “view_time” column in seconds and does it imply total duration users have watched a video?

If you’re referring to LAST VIEW TIME in the CSV file resulting from running this script then, as the column name and code imply, it’s the timestamp of the last time the video was watched by given user ID.

Got it. Thank you for the help!

Hello @jess,
I followed your suggestions and converted the code into Python. For the “Last_View_Time” data, this is the response that I get in Developer console. I can see that all user_id have viewed content but the object that has “Last_View_Time” data is empty. What is it suppose to mean?

Hi @testKaltura21 ,

It means the player they used does not have the history plugin enabled. This metric is based on reporting what point of the video they watched last.

@jess Is there a way to this modify this script (or report) so that the domain is included in the data?