If for some reason you need to do it via code, I would go with this:
Open Sitecore.Analytics.Tracking.config
and find following node
<startAnalytics>
<processor type="Sitecore.Analytics.Pipelines.StartAnalytics.CheckPreconditions, Sitecore.Analytics" />
<processor type="Sitecore.Analytics.Pipelines.StartAnalytics.CreateTracker, Sitecore.Analytics" />
<processor type="Sitecore.Analytics.Pipelines.StartAnalytics.StartTracking, Sitecore.Analytics" />
</startAnalytics>
Add additional processor after CheckPreconditions
or try to overwrite StartTracking
processor.
There is following code inside:
Tracker.Current.StartTracking();
Try to wrap this fragment with your custom conditions.
EDIT:
Inside that method there is a fragment for checking condition from an item (<tracking ignore="1" /
)
private static bool IgnoreCurrentItem()
{
Item obj = Sitecore.Context.Item;
if (obj != null)
{
TrackingField trackingField = TrackingField.FindTrackingField(obj);
if (trackingField != null)
return trackingField.Ignore;
}
return false;
}