This repository has been archived by the owner on Dec 6, 2019. It is now read-only.
/
TrackerService.cs
129 lines (113 loc) · 4.54 KB
/
TrackerService.cs
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
namespace Sitecore.Foundation.SitecoreExtensions.Services
{
using System;
using System.Globalization;
using Sitecore.Analytics;
using Sitecore.Analytics.Tracking;
using Sitecore.Configuration;
using Sitecore.Diagnostics;
using Sitecore.Foundation.DependencyInjection;
using Sitecore.Marketing.Definitions;
using Sitecore.Marketing.Definitions.Goals;
using Sitecore.Marketing.Definitions.Outcomes.Model;
using Sitecore.Marketing.Definitions.PageEvents;
[Service(typeof(ITrackerService), Lifetime = Lifetime.Transient)]
public class TrackerService : ITrackerService
{
public TrackerService(IDefinitionManager<IPageEventDefinition> pageEventDefinitionManager, IDefinitionManager<IOutcomeDefinition> outcomeDefinitionManager, IDefinitionManager<IGoalDefinition> goalDefinitionManager)
{
this.PageEventDefinitionManager = pageEventDefinitionManager;
this.OutcomeDefinitionManager = outcomeDefinitionManager;
this.GoalDefinitionManager = goalDefinitionManager;
}
public IDefinitionManager<IGoalDefinition> GoalDefinitionManager { get; }
public IDefinitionManager<IPageEventDefinition> PageEventDefinitionManager { get; }
public IDefinitionManager<IOutcomeDefinition> OutcomeDefinitionManager { get; }
public bool IsActive => Tracker.Enabled && Tracker.Current != null && Tracker.Current.IsActive;
public virtual void TrackPageEvent(Guid pageEventId, string text = null, string data = null, string dataKey = null, int? value = null)
{
Assert.ArgumentNotNull(pageEventId, nameof(pageEventId));
if (!this.IsActive)
{
return;
}
var pageEventDefinition = this.PageEventDefinitionManager.Get(pageEventId, CultureInfo.InvariantCulture);
if (pageEventDefinition == null)
{
Log.Warn($"Cannot find page event: {pageEventId}", this);
return;
}
var eventData = Tracker.Current.CurrentPage.RegisterPageEvent(pageEventDefinition);
if (data != null)
{
eventData.Data = data;
}
if (dataKey != null)
{
eventData.DataKey = dataKey;
}
if (text != null)
{
eventData.Text = text;
}
if (value != null)
{
eventData.Value = value.Value;
}
}
public void TrackGoal(Guid goalId, string text = null, string data = null, string dataKey = null, int? value = null)
{
Assert.ArgumentNotNull(goalId, nameof(goalId));
if (!this.IsActive)
{
return;
}
var goalDefinition = this.GoalDefinitionManager.Get(goalId, CultureInfo.InvariantCulture);
if (goalDefinition == null)
{
Log.Warn($"Cannot find goal: {goalId}", this);
return;
}
var eventData = Tracker.Current.CurrentPage.RegisterGoal(goalDefinition);
if (data != null)
{
eventData.Data = data;
}
if (dataKey != null)
{
eventData.DataKey = dataKey;
}
if (text != null)
{
eventData.Text = text;
}
if (value != null)
{
eventData.Value = value.Value;
}
}
public void TrackOutcome(Guid outComeDefinitionId)
{
Assert.ArgumentNotNull(outComeDefinitionId, nameof(outComeDefinitionId));
if (!this.IsActive || Tracker.Current.Contact == null)
{
return;
}
var outcomeDefinition = this.OutcomeDefinitionManager.Get(outComeDefinitionId, CultureInfo.InvariantCulture);
if (outcomeDefinition == null)
{
Log.Warn($"Cannot find outcome: {outComeDefinitionId}", this);
return;
}
Tracker.Current.CurrentPage.RegisterOutcome(outcomeDefinition, "USD", 0);
}
public void IdentifyContact(string source, string identifier)
{
if (!this.IsActive)
{
return;
}
Tracker.Current.Session.IdentifyAs(source, identifier);
}
}
}