Just want to know how can we get total number of hits per page for per user through apex class. My apex code is as below:

public class EditCustomerData
    integer count=0;
    public void trackVisits()
        Tracking_1__c track=new Tracking_1__c(
        insert track;

My visual force code is as below:

<apex:page controller="EditCustomerData" showHeader="true" action="{!trackVisits}">


But every time count values is 1. Is there any other way to achieve the same thing?

  • If you can wait for the Summer '17 release, you'll be able to use Visualforce Metrics. See this article by Bob Buzzard: bobbuzzard.blogspot.co.uk/2017/06/…
    – Dan Jones
    Commented Jun 6, 2017 at 14:51
  • Let's say two users access the same page, then what is your expectation. should there be 2 records in Tracking_1__c object or there should be one record but count should be 2? Commented Jun 6, 2017 at 15:31

1 Answer 1


count will always be 1 because when the class is called, you're declaring a new Integer as 0, adding 1 and creating a new record with the value 1.

From what I can see, there's one of two scenarios you want:

  1. Create a new record when a visit is recorded
  2. Get a User's Tracking_1__c record for a particular page and increment their count.

First Scenario

In the first instance, you could literally, each time a User visits a page, create a record:

public class EditCustomerData {
  public void trackVisits() {
    Tracking_1__c track = new Tracking_1__c(
      Users__c = UserInfo.getUserId(),
      Datetime_Visited__c = datetime.now(),
      VF_Page_Visited__c = 'apex/EditCustomerData',

    INSERT track;

Then you can write a simply query to see how often a page is visited:

Integer count = [SELECT Count() FROM Tracking_1__c WHERE User = :myUser AND VF_Page_Visited__c = '/apex/EditCustomerData'];

There's an argument to say you don't need the Datetime_Visited__c because the CreatedDate would be just that.

Second Scenario

You would need to do a few things:

  1. Check if a User has a record for a page
  2. If they don't, create it, otherwise get it
  3. Get the Count__c value
  4. Add 1
  5. Update it

It might look like below:

public class EditCustomerData {
  public void trackVisits() {
    // Get a list of tracking records
    Tracking_1__c[] trackingRecord = [SELECT Counter__c FROM Tracking_1__c WHERE Users__c = :UserInfo.getUserId() AND VF_Page_Visited__c = '/apex/EditCustomerData' LIMIT 1];

    if (trackingRecord.size() > 0) { // Did the query return a record?
      Integer counter = trackingRecord[0].Counter__c; // Assign counter to an Integer
      counter = counter + 1; // Add 1
      trackingRecord[0].Counter__c = counter; // Set counter to the new count
    else { // If there's no record...
      trackingRecord.add(new Tracking_1__c( // ... Create a new one
        Users__c = UserInfo.getUserId(),
        Datetime_Visited__c = datetime.now(),
        VF_Page_Visited__c = '/apex/EditCustomerData',
        Counter__c = 1;

    UPSERT trackingRecord; // UPSERT (Update or Insert a record)

The only problem I could see here is that Datetime_Visited__c becomes redundant. It will just show the last time a User visited the page.

As I mentioned in the comments, you might be better waiting for Summer '17 as they're doing something that will track views to a Page. Although, from what I've experimented with doesn't show it by User, only the total.

  • Thanks Dan for your response. You solved my query. Just want to know if we call any method on Page Action, will it impact on any performance issue ? Commented Jun 7, 2017 at 11:35
  • One more thing Dan, in 2nd scenario, first time when we call trackVisits method, there will be no records in Tracking_1__c table so it will throw error : List has no rows for assignment to SObject. Commented Jun 7, 2017 at 11:57
  • On your second comment, you're right it would. I've updated the answer that should handle that. On your first point, it will affect performance because you are making a callout to the server each time the page is loaded. However, I wouldn't expect something this simple to have much of an impact. Mark this as the answer if it's helped enough! :)
    – Dan Jones
    Commented Jun 7, 2017 at 13:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .