Sitecore analytics trigger goal and add visitor tag

Hi,

In order to achieve personalisation, sometimes triggering goal programmatically is necessary. The analytics API also provides a way to add tag to visitors. This is useful to differentiate between member types.

Then the presentation components can be personalised based on the goal or the visitor tags.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public void TriggerMemberLoginGoal(string goalId, bool addVisitorTag)
{
    //Start tracking if not active
    if (!Tracker.IsActive)
    {
        Tracker.StartTracking();
    }
 
    if (Tracker.IsActive && Tracker.CurrentPage != null)
    {
        Sitecore.Data.Items.Item goalToTrigger = Sitecore.Context.Database.GetItem(goalId);
        if (goalToTrigger != null)
        {
            Sitecore.Analytics.Data.Items.PageEventItem registerthegoal = new Sitecore.Analytics.Data.Items.PageEventItem(goalToTrigger);
            Sitecore.Analytics.Data.DataAccess.DataSets.VisitorDataSet.PageEventsRow eventData = Tracker.CurrentPage.Register(registerthegoal);
            eventData.Data = goalToTrigger["Description"];
            Tracker.Submit();
        }
 
        if (addVisitorTag)
        {
            VisitorDataSet.VisitorTagsRow row = Tracker.Visitor.Tags.Find("IsMember");
            if (row != null)
            {
                Tracker.Visitor.Tags.Set("IsMember", "TRUE");
            }
            else
            {
                //add member tag
                Tracker.Visitor.Tags.Add("IsMember", "TRUE");
            }
        }
    }
}
 
public void RemoveMemberLoginVisitorTag()
{
    //Start tracking if not active
    if (!Tracker.IsActive)
    {
        Tracker.StartTracking();
    }
 
    if (Tracker.Visitor != null)
    {
        Tracker.Visitor.Tags.Set("IsMember", "FALSE");
    }
}
There you go.

HTH,
Andreas

Comments

Popular posts from this blog

SharePoint 2013 anonymous access add attachments to list item

CRM Plugin - Parent and Child Pipeline