I found and use this wonderful Rest API script to create a Hitcounter for my SharePoint Site page, which works as advertised. It adds the Page URL, Viewer’s Name, and Date to a SharePoint List and display the count in a div on the site page. I have tried for days to make this work when a viewer/visitor selects the play button to view an embedded HTML5 Videos displayed on a site page. I would appreciate any ideas on how I can modify the provided Rest API script to target the video on the page instead of the site. Any assistance with this matter will greatly be appreciated.
function loadImages() {
jQuery(document).ready(function(){
console.log('Enterned');
ExecuteOrDelayUntilScriptLoaded(getLoggedInUser, "sp.js");
hitCounter();
});
}
function hitCounter() {
ExecuteOrDelayUntilScriptLoaded(function(){
var LoggedInName;
var URLLink = window.location.href.toLowerCase();
getLoggedInUser().done(function(LoggedInNameTemp){
LoggedInName = LoggedInNameTemp;
checkCounterVal(function(retCntrVal) {
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('HitCounter');
var itemCreateInfo = new SP.ListItemCreationInformation();
var oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Title', "PageVisited");
oListItem.set_item('URL', URLLink);
oListItem.set_item('Counter', retCntrVal+1);
oListItem.set_item('Time', new Date($.now()));
oListItem.set_item('User', LoggedInName);
oListItem.update();
clientContext.load(oListItem,'Include(Title, URL, Counter, Time, User)');
clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));
}); //checkCounterVal closed
}).fail(function(error){
alert(error.get_message());
}); //getLoggedInUser closed
},"SP.JS");
}
function onQuerySucceeded() {
//alert("Page visit value increased by 1");
}
function onQueryFailed(sender, args) {
//alert('Request failed to save page visit counter: ' + args.get_message() + ' \n and \n ' + args.get_stackTrace());
}
function getLoggedInUser(listTitle,propertiesToInclude)
{
//TimeCreated
var clientContext = new SP.ClientContext.get_current();
currentUser = clientContext.get_web().get_currentUser();
clientContext.load(currentUser);
//clientContext.executeQueryAsync(Function.createDelegate(this,onRetUsrQuerySucceeded), Function.createDelegate(this,onRetUsrQueryFailed));
var d = $.Deferred();
clientContext.executeQueryAsync(function() {
var result = currentUser.get_title();
d.resolve(result);
},
function(sender,args){
d.reject(args);
});
return d.promise();
}
function checkCounterVal(callback) {
var retVarNm = 0;
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('HitCounter')/Items?$select=Counter&$top=1&$orderby=Created desc",
method: "GET",
cache: false,
headers: { "accept": "application/json;odata=verbose" },
success: function (data) {
for (var i = 0; i < data.d.results.length; i++) {
if(data.d.results[i].Counter != null) {
retVarNm = data.d.results[i].Counter;
$("#pageVisitCount").text("Page Hit: " + retVarNm);
}
}
callback(retVarNm);
},
error: function (data) {
alert("Failed to check counter");
}
});//ajax closed
}