Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to understand the process of how we can count the number of a specific visualforce page opened by a user. The reason for this question is to see if i can find out what are the pages mostly used by users and enhance it so that i can make users life easier :).

What i know is we need to use some kind of function when page is loading. If there is any otherway i would love to understand it. If you have questions please feel free to ask, i know sometimes we can't put something in writing what we have in our mind.

How does actually visualforce page work?

share|improve this question
Cross-posted from… ;) – eyescream Dec 23 '13 at 20:18
@eyescream Just seen the original post and your Google Analytics idea, great minds! ;-) – Andrew Fawcett Dec 23 '13 at 21:53
up vote 3 down vote accepted

With Spring'14 Salesforce has announced a Custom Metrics Pilot which will provide this information.

In Spring '14, you can collect detailed usage metrics from each organization in which your managed package is installed. By analyzing this information, you can gain valuable insights into the utilization and performance of your app across your entire customer base.

You can collect the following daily metrics on two types of components in a managed package.

  • Custom objects — the total number of records stored per organization in each custom object. This enables you to track how the usage of that custom object is growing with time in any subscriber organization, which is a reliable indicator of how much it's being utilized
  • Visualforce pages — the number of times per organization each Visualforce page was accessed, the number of unique users who accessed it, and the average loading time (in milliseconds). By comparing the metrics for different Visualforce pages, you can determine the relative popularity of different parts of your app in a specific customer organization, as well as trends across all customers.
share|improve this answer

Google Analytics.

You could consider treating your VF pages, like any other HTML page and embed Google Analytics API into the page, the information capture is sent to your designated Google account, for you to review via their dashboards. Perhaps wrapping the use of the API in a Visualforce Component to ensure its easy to place on your pages by your developers.

Google Analytics provides easy to use APIs and SDKs to send data to Google Analytics. With the collection APIs and SDKs, you can measure how users interact with your content and marketing initiatives. Once implemented, you will be able to view user-interaction data within Google Analytics or through the Reporting APIs.

There are further examples of the JavaScript library Google provide, showing how to attach it to events on your page. There is also a setting to configure an opt-out mode depending on the privacy settings you provide in your app.

share|improve this answer

You can add an action method that will allow you to call a DML operation when the page loads. This works even on PDF rendered pages, etc. You can be as granular as you would like. I would recommend that you create an "extension" that would allow you an easy way to add the view counter to each page. The design could look like this:

public without sharing class ViewCount {
    public ViewCount(ApexPages.StandardController controller) { // Required with StandardController

    public ViewCount(CustomController controller) { // Required for custom controller; repeat as necessary

    public void updateCount() {
        Url thisUrl =   Url.getCurrentRequestUrl();
        String thisPath = thisUrl.getPath(), thisFile = thisUrl.getFile(), thisFullPath = thisPath + '/' + thisFile;
        Page_View__c[] views = [SELECT Id, URL__c, Count__c FROM Page_View__c WHERE URL__c = :thisFullPath LIMIT 1];
        Page_View__c thisView = views.isEmpty() ? new Page_View__c(Url__c=thisFullPath, Count__c=0) : views[0];
        upsert thisView;

Then, on each page, simply add the action method:

<apex:page standardController="Account" extensions="ViewCount" action="{!updateCount}" ...

Some pages that already have an action may not be compatible this way, in which case you'll probably want to make updateCount an actionFunction that is called when the page is initially loaded:

<apex:actionFunction name="updateCount" action="{!updateCount}" reRender="" />
    addEventListener("load", updateCount, false);

You'll need a "form" tag if you don't already have one in order to call an actionFunction; you can wrap it around the actionFunction or around the entire contents of the page, whichever you prefer (but the former will produce a far smaller view state in most cases).

All objects mentioned here that do not exist will need to be created first. Users won't need permission to access the object, since it is updated "without sharing."

share|improve this answer
Usual caveat - wouldn't recommend this for managed packages - they get very sniffy about the action tags on pages... – BritishBoyinDC Dec 23 '13 at 20:17
True. It would also raise flags for the class' security being "without sharing" for AppExchange deployments; this answer presumes that they are under control of the code and have no intent on deploying the code publicly. – sfdcfox Dec 23 '13 at 20:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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