How to enable/disable visits tracking from content database.

I has been ask by business to find out a way to not track internal visits – easy stuff – we can add internal IP, public IP into <excludedIPAddresses> node of Sitecore.Analytics.ExcludeRobots.config file.

 

It worked for a while. Then I has been ask to disabled only on CM, but     …sometimes 😉 . Marketers, usually don’t want to see internal traffic to be tracked, but from time to time they would like to test some functionalities and  to track visits.  Deploying each time a config file is not an option.

I thought, it would be nice to add some logic into  the same pipeline (excludeRobots) where we filter already IP to exclude.

Here is my solution (POC, Production code has been optimized) :

  1. I added new processor to a pipeline
 <excludeRobots>
 <processor type="Sitecore.Analytics.Pipelines.ExcludeRobots.TryObtainCachedResult, Sitecore.Analytics"/>
 <processor type="Sitecore.Analytics.Pipelines.ExcludeRobots.CheckUserAgent, Sitecore.Analytics"/>
 <processor type="XXX.SitecoreXP.Pipelines.InternalTrafficCheck, XXX.SitecoreXP.Pipelines"/>
 <processor type="Sitecore.Analytics.Pipelines.ExcludeRobots.CheckIpAddress, Sitecore.Analytics"/>
 <processor type="Sitecore.Analytics.Pipelines.ExcludeRobots.AddResultToCache, Sitecore.Analytics"/>
 </excludeRobots>

2. Processor code

public override void Process(ExcludeRobotsArgs args)
 {
     if (Sitecore.Context.Database != null || Sitecore.Context.Database.Name.Equals("master", StringComparison.InvariantCulture))
     {
      var settingsItem = Sitecore.Context.Database.GetItem(
 "/sitecore/system/Settings/Analytics/Custom Settings/Analytics Settings");
      if (settingsItem != null)
      {
          var field = (CheckboxField)settingsItem.Fields["TrackLocalTraffic"];
          if (field != null)
          {
           if (!field.Checked) // add your own Business Logic
             {
              args.IsInExcludeList = true;
             }
           }
       }
    }
 }

3. Add setting to content database

traffic

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s