1

I'm looking for a way of displaying on every page the number of visits there have been. i.e. A hit counter.

I'm using Sharepoint online, and the option of coding our own web part has been ruled out.

Does anyone know a way of doing this 'out of the box'?

2 Answers 2

2

You can write some simple JSOM script to achieve this functionality. Since you are using SP 2013 , you can leverage Search service to get the number of visits for any page.

  1. In a Javascript file , write a method which use KeywordQuery to query for the current page and retrieve the ViewsLifeTime property value and dump this value in some UI element in the masterpage.(Put this js file in style library)
  2. Reference this Javscript file in the masterpage.

Below is the sample code for same:

 var context;
    var web;
    var list;
    var currentItem;

    $(document).ready(function () {
         //Make sure sp.js ,sp.runtime.js and sp.search.js are loaded
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
        SP.SOD.executeFunc("SP.Search.js", "Microsoft.SharePoint.Client.Search.Query.KeywordQuery", LoadCurrentPageID);
    });
    });

function LoadCurrentPageID()
{
    context = new SP.ClientContext.get_current();
    web = context.get_web();
    list = web.get_lists().getById(_spPageContextInfo.pageListId);
    currentItem = list.getItemById(_spPageContextInfo.pageItemId);
    context.load(currentItem);
    context.executeQueryAsync(onQuerySucceeded, onQueryFailed);
}


    //Get the page guid and perform a keyword query such that only the current page is returned.
    function onQuerySucceeded() {
        var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(context);
        keywordQuery.set_queryText('PageGuid:"{' + currentItem.get_fieldValues("UniqueId").UniqueId.toString() + '}"');
        var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(context);
        results = searchExecutor.executeQuery(keywordQuery);
        context.executeQueryAsync(SearchDone, SearchFailed)
    }
    function onQueryFailed(sender, args) {
        //Error Logging
        console.log(args);
    }
    function SearchDone() {
        var viewCount = results.m_value.ResultTables[0].ResultRows[0].ViewsLifeTime;
        //Dump the view count onto the UI element in the page
        $('#spanViewCount').text(viewCount);
    }

    function SearchFailed(sender, args) {
        //Error Logging
        console.log(args);
    }

Prerequisites:

  1. Make sure Search is configured with continuous crawl.
  2. Load sp.js,sp.runtime.js and sp.search.js in the page
1
  • Thanks, I was looking for something a little more 'end user friendly', any coding solution is frowned upon, but if nothing else presents itself, I'll give this try. Jun 8, 2015 at 9:53
0

Add a Content Editor web part at the bottom of the page.

Edit the source of web part and reference your js file like ​<script src="/sites/subsite/SiteAssets/functions.js" type="text/javascript"></script>

And in the js file add:

ExecuteOrDelayUntilScriptLoaded(init,'sp.js');
function init(){
    // code to update counter
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.