Hit counter for SharePoint

28.05.2011 I have redone this solution to use a star-rating in stead of the simple “like” button. You find the new version here


01.11.2010 Redone the scripts and added support for rating individual items in a list – for example in “Dispform” in a blog:
IMG

The solution counts “Hits” and “Likes”.


Create a document library to hold your scripts

Or a folder on the root created in SharePoint Designer). In this example i have made a document library with a relative URL of “/test/English/Blog/Scripts” (a sub site named “test” with a sub site named “English” with a subsite named “Blog” and a document library named “Scripts”).

When you refer the scripts in the CEWP, ensure that you point to the correct location of your scripts.

The jQuery-library is found here. The CEWP code refers to jquery-1.4.2.min. If you download another version, be sure to update the script reference in the CEWP.

The sessvars.js is created by Thomas Frank and can be found here

The sourcecode for the file “HitCounterForSharePoint.js” is found here

Create a list with the name “HitCounter”, and add these fields:

  • URL: Multiline plain text
  • User: Person or Group
  • ReferringURL: Multiline plain text
  • Like: Single line of text

You have to give all users write access to the list “HitCounter”.

Add a CEWP where you want this counter to appear, and add this code:

<script type="text/javascript" src="/test/English/Blog/Scripts/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="/test/English/Blog/Scripts/sessvars.js"></script>
<script type="text/javascript" src="/test/English/Blog/Scripts/HitCounterForSharePoint.js"></script>
<script type="text/javascript">
	hitCounter(true,true);
</script>

Parameters:

  • oncePerSession: True to allow one “hit-count” per unique item per browser session.
  • isDispForm: If true, includes the ID of the item to allow individual counting of each item in a list.

Ask if something is unclear!

Regards
Alexander

169 thoughts on “Hit counter for SharePoint”

      1. Hi,
        Do you have the list that counts the hits in the same site as you are using it? You will need a small change in the script to have the counter list in for example the root site and then use it in a subsite.

        Alexander

      2. Can you please clarify this statement?
        Do you have the list that counts the hits in the same site as you are using it? You will need a small change in the script to have the counter list in for example the root site and then use it in a subsite.

      3. The list “HitCounter” that you create to hold the comments: is it in the same site (look under “All site contents”) as the list or page where you have the ratings?

        Alexander

      4. Hi;
        Thank you for the reply and for your assistance. This is how I have the files organized:
        Home site
        ->Documents (all scripts located here)
        ->Lists
        ->HitCounter

        I put the CEWP on the Home Page with the following (on the homepage):

         <script type="text/javascript" src="/sites//Scripts/jquery-1.4.2.min.js">
         <script type="text/javascript" src="/sites//Scripts/sessvars.js">
         <script type="text/javascript" src="/sites//Scripts/HitCounterForSharePoint.js">
         
         hitCounter(true,true);
         
         
      5. Corrections:
        Hi;
        Thank you for the reply and for your assistance. This is how I have the files organized:
        Home site
        ->Documents->Scripts (folder) (all scripts located here)
        ->Lists
        ->HitCounter

        I put the CEWP on the Home Page with the following (on the homepage):

        
        
        
        
        hitCounter(true,true);
        
        
  1. this was real easy to implement. I made some changes and will try to document. Starting with the fields
    » URL: Singleline text – just to start, will make Multipleline later
    » User: Person or Group
    » ReferringURL: Singleline text – just to start, will make Multipleline later
    » Page: Calculated Field = to URL
    once this field is created Change URL to Multipleline. The reason for this is a calculated field will not read multipleline field by default. This method tricks SP into reading it. This also give us the ability to group by Page
    » Day: calculated field =TEXT(WEEKDAY([Modified],”ddd”)) this allows me to start seeing trends by day of the week.

    with this configuration I can now create a group view by Page, then User. With this I implemented your script for counting groups here:

    https://spjsblog.com/2009/11/25/count-groups-in-grouped-list-view/

    of course there are many different techniques this script was very helpful becaue of the data is supplied and setting up the views

    1. Hi,
      Are your visitors anonymous or do they have to sign in? If they are anonymous, do they have restricted read access?

      If you want to count hits (without using the built-in Site usage report) the visitor must have write access to the counter.

      You may use some server side code to do this with elevated privileges, but there i cannot help you.

      Alexander

  2. Hi Alex,

    Is there by any chance this to be used to let you know how many users are online at the moment on a particular page? Like who’s or how many people are online on this page at the moment or to be checked on an interval of time?

  3. Hey I was wondering why in the “Title” part of my “HitCounter” list, it just displays “Site:” and not the actual title of the page ?

    Any help would be great, thanks Alex!

    – Alex F

    1. Hi,
      The script pulls the title from

      $("td.ms-sitetitle a").text();
      

      This is the title of the site displayed in the “title area”. Is the page with the counter script attached to a master page?

      Alexander

    1. No need to put anything in the master page. I just wanted to know if the page was a standard SharePoint page.

      Does your page have a title? – it’s not just a logo? – check it under “Site settings” > “Title, Description, and Icon”.

      Alexander

    1. Try to alert

      $(".ms-pagetitle a").text();
      

      What do the alert say?

      Do you require the site title or the document library title? – the URL is recorded in the “URL” column….

      Are all the other values there?

      Alexander

    2. Hi Alex,
      I misled you a bit here, to get the list or library title:

      $("td.ms-pagetitle").text();
      

      There is no “a” in the pagetitle.

      Alexander

  4. Hi Alex,

    I am new to Shartepoint. Please help me to create a webpart which has

    •The ability to historically track number of users (visitors) on a site prior to and after the current month
    •The ability to historically track number of users (visitors) on a library prior to and after the current month
    •The ability to historically track number of users (visitors) on a list prior to and after the current month

    i tried the above example and nothing is showing in the CEW. Do i need to do any other settings (like publish,etc..)

    Thanks,
    Satheesh

  5. Is there a way to only show the visits from one person on a day, for some reason its showing the same user twice if the user refreshes the page?

  6. hi, i solved that.
    turns out i my interaction.js and stringBuffer.js was corupted.
    I think if it was available in your site my life will be longer about 2 hours 🙂

    PS. My eng is bad..

  7. I have one more question.
    With this code additem() function adds items only in the list in this site.
    Is it posible what to add items in the same list from other site/site collection?

  8. I must be missing something. I updated all the paths to point to my script library and I have all the scripts in there, but my CEWP is blank. Is there someplace that I’m supposed to point to my hit counter list? I know I must be overlooking something obvious.

  9. I was about to respond a few minutes ago that I couldn’t get this to work but it now works and I think it is FANTASTIC! Alex, thanks for making this available for free.

    Just to help others who might run into the same problem as I did – make sure you download the right script from the links above.

    Alex, thanks once again.

  10. First of all, thanks for this fantastic tool. It’s very useful, works very good and I easily personalized it to extract data I need to populate my HitCounter list.
    I would like to ask to you what do you think about scalability: what, for example, if 200 users try to access to a page in the same time?

    1. Hi,
      This would be equivalent to 200 user submitting a new item to a list using NewForm.aspx. I do not think this would be a problem, but i have not “stress tested” it.

      Alexander

  11. Hi Alexander,

    *sigh* I can’t get this to work. I’ve updated the paths, checked with alert, the function is called, though nothing is written to the HitCounter List. “Public” has r/w access to the list and it’s on the same site. Can you think of anything else that might be wrong? I even re-downloaded the other three .js files from their orginial location, nothing changed though.

    Chris

    1. Hi,
      Hard to tell… Have you tested it in another location/page?

      Double check that you use the right DisplayName for the “HitCounter” list as it is addressed using the DisplayName.

      Alexander

  12. Hi Alex,

    Is it possible to use this counter in a site where CKS EBE (Community Kit for Sharepoint Enchanced Blog Edition) is enabled as a site feature. I’ve tried messing with the theme.master file found in one of the themes folder but to no avail.

  13. Hi this article helped me a lot .. Thanks for that.

    I implemented your code and worked fine and i like to ask couple of questions regarding that..

    1)Can we have like “Update Item” also so we no need to store when each time the user hit the page .?
    Here is my scenario.. I have blog and i am sure everyday at least 100 users will hit article pages.. If we store the way we are storing now that’s going to be huge data on “Hit Counter” list.. is that the right scenario? is there anything we can update the current values to increment?

    1. Hi,
      To achieve this you would have to write a logic that overcomes the possible version conflict if multiple users write to the same object at the same time.

      It is not supported at the moment, but i do not think this will be a problem. Your “counter list” can hold 5 million items…

      Alexander

  14. Hello,

    This article is really useful! But I have a problem with the implementation: it works fine for an authenticated user but for an anonymous user the function returns -1 hits… Can you help me on that?

    Bianca

    1. I did that… I gave anonymous full control on the counter list… As I was saying… It works fine for an authenticated user but not for anonymous… I’ve tryed to access the list from the browser with anonymous and I managed to add a new item to it… any other ideas?

      Thanks,
      Bianca

    2. Hi,
      I’m not so familiar with anonymous access, but you might get it working if you remove the “setRequestHeader” in the ajax query. You find this in the function “innerPost” in interaction.js. Copy this function into your code and modify it by commenting out the line

      xhr.setRequestHeader('SOAPAction', actionHeader);
      

      Alexander

  15. Thanks Alexander,
    Just tried the updated Hits & Like feature for Blog Posts, perfect, works great!
    Server-side web-part installs? Who needs ’em eh 😉
    I bask in your awesomeness.
    Much appreciated, Brett.

  16. The code is working fine for adding records to the HitCounter list. However, I’m getting a -1 hits and -1 likes. My other sites are fine, but my main site isn’t working properly. Again, the list is being updated, just not displaying the counts.
    Much appreciated.
    Jim

    1. Hi,
      The hit counter stores each “hit” or “like” in a separate line in the “hit counter list”. To retrieve this you would have to write a script to pull them in and count the hits and likes on the individual items. This might take a few seconds, but is no big deal.

      The problem is that as the script is setup right now, it does not store the “title” for each record – only the URL. To have the “Most viewed” display in a proper manner, you would have to do another query against the list where the hits are being registered to fetch the title.

      Again not hard to do, but adds a few seconds to the page render (depending on the number of line in the counter list).

      Alexander

  17. Thanks for the sharing !!
    I have implemented, but hit is showing always 0 and like it button show 1 when I hit on it, but if I refresh it becomes 0 again. I can see everything in the list. Any idea ?

  18. thanks, great work, Right now this thing work for browser session, I want to use for per user, so if user have visited the site once, it won’t take hit or like again. Thank for awesome file.

    1. Tested anonymous access with the v1.5 01.11.2010 code as downloaded. It makes the page request a Sign In (using CAC in our case). Since the anonymous user doesn’t have an account in our AD domain they can’t Sign In to view the page.

      Tried changing function spjs_wrapSoapRequest to comment out this statement…
      xhr.setRequestHeader(‘SOAPAction’,requestHeader);
      That caused an Alert box with the text “Null” on each page refresh.

    2. My testing of anonymous access was flawed. The Contact Details web part (with presence) was causing the anonymous access for the page to fail. After removing it, the v1.5 01.11.2010 code as downloaded allowed anonymous access.

      There is no hit item added to the HitCounter list for an anonymous user, and there is no error generated back to the user. I suspect this is caused by the list access permissions. The “add item” check box appears in the anonymous access options, but only “view item” will remained checked after the selection is saved.

  19. I’m thinking about a modification to use a single HitCounter list for all the data collected in my site collection. I’ve got about 500 individual sites, but less than 50 pages would actually have hit counters. The usage is low. Only about 500 unique users per day and 10,000 requests on a busy day.
    Any thoughts on what problems I migh have?

    1. I do not know for sure, it depends on the amount of hits.

      This being a client side code, the Ajax request has to pull down the xml and parse it locally. It you get thousands of hits on each “page” the data that has to be downloaded to the client can be as much as a few MB.

      I did a quick test querying a list with 8000 items and it resulted in an”xml-blob” approximately 3 MB.

      Alexander

    2. I hadn’t thought about the size of the blob coming back. That might be “hidden” network traffic that we don’t want. It would be large compared to the page transfer size.

      We’re only getting about 300 hits logged per day for the 5 pages that interest me. That’ll increase if I open the capability to all my site managers. Probably never top 1k hits logged per day, but we’d get to a large blob in a few weeks with a central list. We’ve got a Gb network, but it’s still a lot of extra traffic added to peak periods.

      Isn’t there a way to do a query that only returns the count?

    3. I added this script to a few pages when A first posted it. My SP admin was not happy for multiple reasons. 1. this will continually add hit in the list and we have no way of knowing how many. 2, currently there is no way to automatically archive this data. This this becomes a manual effort. 3. it could reduce performace depending on the usage the hit counter takes.

      Now I still have it implemented and only use it on a few pages. Also I only keep about 90 days od data (1 quarter) but this again is a manual effort to delete the old data.

      This is a great script and the concept is awesome. I dont want to take anything away from A’s work. Just some input from someone currently using the script.

  20. Hi Alex,

    This is a wonderful post. but i am facing the same issue as ‘wakeup009’ .

    I have created list with columns as u mentioned. and the hits showing up in hitcounter list. but when i open the page it is not rendering the count from list. i use IE8. please help.

    1. Hi,
      Do you get any errors?
      Is the site on a managed path?
      Is it SP2007 or 2010?
      What rights do you have to the site (read, contribute or owner)

      If you answer these questions, i might be able to help you further.

      Alexander

    2. Does the record in the list “HitCounter” show the exact relative URL of the page where the counter is located (In the “URL” field)?

      Alexander

    3. Awesome work; however, I’m getting the same error as naveen. Here are my answers to A’s question:

      1) no errors
      2) yes the site is on a managed path
      3) SP 2003, IE6
      4) Full Control

    4. Also, the RelativeURL is not always populating in the hit counter list…it is there for maybe 10% of the entries!

      It’s still great to have the HitCounter List by itself, though 🙂

  21. The solution is very simple, just make sure you create the column as a plain text, (URL & ReferringURL) and if you create the rich text then you will get always 0 when you refresh the page.

  22. Very nice solution. We probably going to implement this on our intranet (though slightly different: no usernames and no “I like it”).

    But this is a very good starting point.

  23. Hi Alexander,
    I implemented your code and all I am getting is -1 hits and -1 likes
    I am not getting what should be needed.

    I want this counter to work for anonymous User.
    Any Help?

    Thanks & Regards,
    Alekhya.

    1. Hi,
      Anonymous users is a bit tricky as they would have to have write access to the hit counter list. It was originally thought to work with anonymous users, but the appropriate rights has to be added.

      The -1 hits could be the hit counter list name not being exactly right, or the list is not in the current site.

      Alexander

  24. I have a question in reference to links on a site. I would like to know if it is possible to ‘track’ the link usage. We are a military hospital and using MOSS for our Intranet, I would like to see how often the links are access.

    1. Hi,
      You could do this by adding a counter to the “target” of the link and then look at the hit counter list for “ReferringURL”.

      Alexander

    1. Hi,
      You would have to add some parameters to the script. I will incorporate this in the next release – i have started to look at the star rating in stead of the “like-button”.

      Alexander

  25. Hi All, I’ve got a useful tip that can be used with the Hit-Counter using Filtered Views.
    You can create a web part on your main page for an enhanced Greetings “User” message and Your Last Visit Was: “date”.

    – Create a View from within the HitCounter list.
    – Select User and Created check box.

    -Sort by: Created (Descending Order)

    – Show items only when the following is true: Column “User” Is Equal To [Me]
    and
    Column “Created” Is Less Than [Today]

    – Limit the total number of items returned to the specified amount. = 1

    – Hide the Hit-Counter on your Default.aspx page.

    So now using some DataView Webpart skills or some basic Rich Text Editor skills, you can display the Greetings “Current User” and/or Your Last Visit Was: “Date”.

    I’ve also been toying with a Visitors Log, which displays a web part list of Users to that page’s URL.
    – Show items when True: URL contains “Page URL”,
    – Group by “Created”.
    Webpart displays column “User” Grouped by Date.

    Anyone else have examples of how this List can be used?

  26. Generally, if you have a -1 your list was created incorrectly with a wrong data type. Recreate the HitCounter list with PlainText fields (not RTF)

  27. Hi Alexander,
    this is a great work! Thanks. Works perfectlly. I would love to use your solution but I would like to modify it in this way: each page has only one item in hitCounter list with a counter field, which is incremented every time the page is open. Could you please give me an advice what do I have to change and how to achive this? Maybe some code snippet? Thanks.

    Lukas

    1. Hi,
      Sorry, this question never got my attention…
      Unfortunately this cannot be done in a good way as multiple users would want to write to that same file at the same time. If we did this, we would possibly loose some hits due to simultaneously updates.

      Alexander

  28. Hi Alex,
    i have a share point site where in i have lot of word documents in it( all present in a list). many people have rights to access to that documents. what i want is if people updates or alters any of the word documents then immediately that should be updated in the home page like ” so and so document is been updated by “x” at time….. can u help me in doing this …….

  29. hey,

    is there any way to connect the “like” with the like-metadata-functionality in sharepoint 2010??

    if not – would you mind publishing a hitcounter without the like-functionality? would be awesome.

    greetings.

  30. Hello,

    When I click on the link to download the source (The sourcecode for the file “HitCounterForSharePoint.js” is found here

    ), I get an error. Is it still available?

  31. Hi!
    I followed instructions. But I get undefined Hit and undefined Like in my CEWP even if i click Like.

    If I go in my HitCounter list, it shows that my page has 1 like.

    Do you know why it is not reflected on my page?
    Thanks 🙂
    -Daniel

    1. Hi,
      Could you post the content of the “URL” field in the record from the list where the hits and likes are stored? – you can strip away the domain name if you like, but the list path and name must be accurate.

      Alexander

  32. Hi Alexander, just wanted to let you know I just implemented the Hit and Like Counter on our intranet (SP2007 MOSS) and it works great. Think I’m becoming some kind of “Alexander groupie”, since I have used a couple more of your script to enhance our article pages :D. Thanks for all the great work!
    Claudia
    (ps If you like a screenshot of our article pages with your scripts, just let me know)

  33. i added this to my site, it doesnt appear when i’m not looged in otherwise works very well
    what could be the problem? any help is much appreciated

  34. This was working very well until we switched to MOSS2010, now I get an error message saying “One or more field types are not installed properly. Go to the list settings to delete thse fields.” HELP!!!!

  35. Hi Alex,
    Firstly i would like to thank you for this solution. it works extremely well.
    i would just like to know what do I need to modify in the code have only the hits and not the like?
    I tried removing the Like column in the hitCounter List but that just made the hit and like set to -1.
    Please help.

  36. Hi Alex – This code is amazing! Great work!

    I ran into a problem with logging the hits. It would log the hits only once per user and I can’t get it to log all hits.
    I have set the oncePerSession value to false but no luck.
    I have tried using the new updated version with the rating stars and set the counters true to log every session but still no luck.

    Can you assist/ give me some insight as to what I might be doing wrong?

    –thank you!

  37. Hi

    I am a novice with js and just want to use the ‘like’ function. I have set everything up as described but dont know how to trigger the javascript from a text link to put an entry into the hitcounter list.

    Can you help? Im using sp designer 2007 and WSS3

  38. Hi I’m still not getting this to work. List is getting updated but i’m getting /TestSite/default.aspx?ID=undefined and also getting -1 Hit and -1 Like. I have created a list with same HitCounter name and all the js file have been copied and path is correct. Please sort this one out. I’ll be thankful to you.

    Regards,
    Anki

  39. Hi Alexander Bautz,
    I have followed the instruction and I am getting undefinedHit undefinedLike in my CEWP.. how to fix this issue.. If possible I wnat to display only total hit count in numbers can it is possible , if Yes then how we can do?

    1. Hi,
      Sorry for the late reply. This solution requires the configuration list to be created in the same site as you want to use it – could this be the issue?

      Also, there is a newer version linked in the top of the article.

      Alexander

    1. Hi,
      I’m not sure I understand what you mean. The “user” is stored in the configuration list in a people picker field. If you want the user ID you can set this field to show the ID.

      You can also change the field type for the “User” field in the configuration list to “Single line of text” to have the ID only.

      Alexander

  40. I also have the ‘undefined hit’ ‘undefined like’ – my scripts library (with scripts) and the hitcounter list are both within the blog site. Double checked the links which are fine. Using more up to date jquery but the reference is correct.

    The list is recording the hits and likes, just not displaying the figures in the buttons.

    Any help greatly appreciated!

  41. The counter is great and I see all the data in the HitCounter list but the counter on the page is always 0 even thought there are hits logged in the list. Any thoughts? Thanks!

  42. Hello;
    One more request for help…I am getting the following error:
    “One or more field types are not installed properly. Go to the list settings page to delete these fields.” I’ve gone over all the info. however am getting the above error as well as -1Hit -1 Votes . This one has stumped me…Any help is much appreciated.
    Fields I am using are:
    •URL: Multiline plain text
    •User: Person or Group
    •ReferringURL: Multiline plain text
    •Like: Single line of text
    With the CEWP code as:
    -1Hit

    -1 Votes

    var argObj = {counterListName:’HitCounter’,

    counterListBaseUrl:”/sites/WME_WoW_Focus_Team”,
    countHits:true,
    hitCounterVisible:true,
    countOneHitPerUser:false,
    countOneHitPerUserPerDay:false,
    hitSuffix:[‘Hit’,’Hits’],
    isDispForm:true,
    activateRating:true,
    starImageSrc:[‘/sites/WME_WoW_Focus_Team/Documents/Scripts/star_off.gif’,
    ‘/sites/WME_WoW_Focus_Team/Documents/Scripts/star_on.gif’,
    ‘/sites/WME_WoW_Focus_Team/Documents/Scripts/star_selected.gif’],
    starCount:5,
    starMouseoverArr:[‘Very poor’,’Poor’,’Good’,’Very good’,’Fantastic’],
    oneRatingPerUser:false,
    ratingCountSuffix:[‘Vote’,’Votes’],
    alreadyRatedMouseover:”You have already given this item {starCount} stars!”,
    debug:false};
    hitCounter(argObj);

      1. Hello;
        I wanted to let you know, I finally was able to get this to work…now that it is, thank you for the scripts 🙂 I had a few missing fields in my custom list….
        Thanks,
        Michele

  43. HI,
    It seems that I am having issues with my parameters:

    hitCounter(true,true)
    var argObj = {
    isDispForm:true,
    oncePerSession:true,

    nothing is displaying on my page when loading and there is nothing in the console log.

    any help would be great

    1. Hi,
      I’m not sure if this is the issue, but you are not supposed to have these lines in the code:

      var argObj = {
      isDispForm:true,
      oncePerSession:true,

      The object is not formatted correctly and can cause an error.

      Hope this helps,
      Alexander

  44. Hi,

    Not sure if you are still supporting this. I’m not having any errors with the code but my counter is just 0. “0 hit 0 like” I have checked the HitCounter list and there are records in the list but it isn’t reflected in the counter displaying. Any suggestions?

      1. Hi Alexander,

        I changed the jQuery version to 1.6.4 as suggested but it is still returning 0. I have looked through the code and debugged the issue down to this line.
        var hitCount = spjs_QueryItemCount({listName:’HitCounter’,query:hQuery}).count;

        This is setting hitCount to 0. I’m unsure if there is an error with this line or if the hQuery is just returning 0.

        The hQuery is
        var hQuery = “<Where><And><Eq><FieldRef Name=’URL’ /><Value Type=’Text’>”+urlDir+”</Value></Eq><IsNull><FieldRef Name=’Like’ /></IsNull></And></Where>”;

        I’m really unsure of what most of this means but if you could provide any context to fix this or information to learn how to write my own query it would be appreciated.

  45. Hi,
    I’m not sure as I have not tested this version lately, but I see from my code example in the article that I used jQuery v1.4.2 and not v1.6.4 – can you test this version to see if this may be down to a change in jQuery?

    You can also use the Network tab in the developer console (hit F12 > Network) to inspect the query and see if you see any errors there.

    To look at the result of the query from your code snippet above, you can change it like this – you must have the developer console open (hit F12 > Console):

    var testQuery = spjs_QueryItemCount({listName:’HitCounter’,query:hQuery});
    console.log(testQuery);

    Alexander

  46. Hi, your work is fantastic!!!
    But i would like to have also a comment field beneath the stars that should also be connected to the same sharepoint list and the same ID like the stars…
    Do you have any solutions or any ideas how i can achieve this?

    Thanks in advance

    1. I found your post for a commentBox for MOSS 2007…
      Is it possible to combine both the commentBox and the star-rating in one sharepoint-list?

  47. Hi,

    I added the code in a webpart page located in one of my document library’s subfolder.

    However, it only shows the Hits as -1 and the Likes also as -1.
    Nothing happens if I click the Like button, as well as the HitCounter list does not track the hits for the page.
    It only tracks the hits of the main page.

    Is there anything I am doing wrong?

  48. I have added this into a page layout, it works as expected on a top level site however on subsites it does not count the hits or store the likes.

    I have tried re creating the list in the subsite as well

    any ideas

    1. Unfortunately I have not had the time to keep this solution up to date – it’s nearly 5 years since I last reviewed it.

      As this solution does not have a “baseUrl” property in the code interacting with the lists, I would guess this solution should work if you added the counter list to the subsite.

      Do you see any errors in the developer console (hit F12 > Console)?

      Alexander

      1. Hi Alexander,

        thanks for your response. I have tried adding in the counter List to the subsides and it is storing counts and likes.

        however on the page it is displaying a 0 hits and 0 likes

        there are no errors in the developer console

  49. Hi!
    It seems that users can register as many Likes as they wish. Once visiting the page, a user can only register ONE like. But the next time the user visits the page, he/she can register a new Like. Even a refresh of the page makes it possible to register more Likes.

    1. Hi,
      It has been almost five years since I did any work on this solution, but from the configuration options it looks like you can only limit the likes within one session (browser window).

      You might have better luck with the the slightly newer solution linked in the top of this page.

      Alexander

  50. I know it’s been almost 5 years since you’ve worked on this code, but we upgraded our servers to 2015, and now the User name is blank, so that it now counts EVERY time someone goes to a site instead of once a day. Any suggestions?

    1. Hi,
      Sorry for the delay. This should be an easy fix. Open the “HitCounterForSharePoint.js” file and replace this:

      if(typeof(_spUserId)!=undefined){
      	userId = _spUserId;
      }

      with this

      if(typeof(_spPageContextInfo.userId)!=undefined){
      	userId = _spPageContextInfo.userId;
      }

      Alexander

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.