Share via


Hit Count Report using script in sharepoint

Question

Monday, February 25, 2019 9:56 AM

Hi,

I have a requirement to record the Monthly Hit count in the sharepoint site. My OOB feature (web analytic report) is broken and not working. So could you please suggest a custom solution? Please share the script if I can add to the Conte Editor webpart.

Gone through lot of technical blogs but didn't find relevant solution.

Thanks.

Knowledge is power.

All replies (4)

Monday, February 25, 2019 3:23 PM

Hi ,

Please check the below similar thread

https://social.msdn.microsoft.com/Forums/en-US/4d04e30c-b694-4340-8fd4-3870bbbec4e3/sharepoint-report-for-number-of-hits-and-sites?forum=sharepointadmin


Tuesday, February 26, 2019 4:39 AM

Hi,

The following example about show Monthly Hit Count of a page.

Create a custom list "HitCounter", the Title field store the current month.

Then add the code below into a script editor web part in SharePoint page(example: home page).

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
    var listName="HitCounter";
    AddHitCount(listName).done(function(data){
        ShowMonthlyHitCount(listName);
    });
});
function ShowMonthlyHitCount(listName){ 
    $.ajax({
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('"+listName+"')/items?$filter=Title eq '"+(new Date().getMonth()+1)+"'",
        type: "GET",               
        headers: {
            "Accept": "application/json;odata=verbose",
        },
        success: function (data) {
            $("#MonthlyHitCount").html("Monthly Hit Count: "+data.d.results.length);
        },
        error: function (data) {
            //alert("Error");
        }
    });
}
function AddHitCount(listName){
    var itemType = GetItemTypeForListName(listName);
    var item = {
        "__metadata": { "type": itemType },
        "Title": (new Date().getMonth()+1)+""
    };
    
    return $.ajax({
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(item),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        }      
    });
}
function GetItemTypeForListName(name) {
    return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
}   
</script>
<div id="MonthlyHitCount"/>

Best Regards,

Dennis

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].

SharePoint Server 2019 has been released, you can click here to download it.
Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


Tuesday, February 26, 2019 1:24 PM

Hi,

I added the script, may be slightly different than your's . Its working fine for my login id. But when other users visit the site the item is not getting added to the list/not counting the hits. Any idea?

Knowledge is power.


Wednesday, February 27, 2019 8:56 AM

Hi,

Please set the user permission for this list.

In SharePoint on-premise, we can create a visual web part and using RunWithElevatedPrivileges to run code as admin user.

Best Regards,

Dennis

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].

SharePoint Server 2019 has been released, you can click

here to download it.
Click

here to learn new features. Visit the dedicated

forum to share, explore and talk to experts about SharePoint Server 2019.