Maksym Ponomarenko
Sitecore Consulting

Use Sitecore Analytics From WCF Service

Sturer Esel

Hi guys, I found an interesting challenge: I tried to use Sitecore Analytics from my WCF service but the Analytics stubbornly didn’t want to be activated.

And when I found a solution, it was a wonderful feeling of catharsis. So let me share it with you. When you want to use Sitecore Analytics from a WCF Service, you will probably face two problems.

 

Problem 1: Sitecore Analytics needs a Site Context

First of all, Sitecore Analytics works with a Site context. This means that you need to define a Sitecore site for your service. To do this just register a new site in the web.config, as in my example below:

In the config file under the “/Sitecore/sites” add a new node

Sitecore Code 1

 

So the site “my_service” is referenced to the MyServices location: virtualFolder="/MyServices"

physicalFolder="/MyServices"

It would work for HTTP handlers and webservices, but WCF service is not allowed by Sitecore by default. Now the request will be filtered. To fix this, go to the Web.config, find a pipeline “preprocessRequest” and in “Allowed Extensions” of the FilterUrlExtensions processor add a “svc” extension

Sitecore Code 2

 

Ok, now the Sitecore context should be fine. But if not, please do not despair. You always can use SiteContextSwitcher

Sitecore Code 3

 

The first problem is fixed.

 

Problem 2: Sitecore Analytics Uses a Session

The second problem is a session. Even after your start Tracking using the method Tracker.StartTracking(), Tracker.Active is still false.

Sitecore Code 4

 

This happens because Sitecore Analytics uses a session. And if your WCF was configured wrong, the session will be disabled.

To fix this, you need to set an Asp.net Compatibility requirements mode to “allow”:

- In an attribute of your service class:

Sitecore Code 5

 

- In the configuration of the WCF:

Sitecore Code 6

 

Now it should work!

Kommentare
Es wurden noch keine Kommentare zu diesem Eintrag geschrieben.
Kommentar hinzufügen
Vor und Zuname
E-Mail
E-Mail bei weiteren Kommentaren
Mein Kommentar