forked from AmitKuma-04/PowerBIPushIntegration
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactTrackerPlugin.cs
More file actions
101 lines (90 loc) · 4.05 KB
/
ContactTrackerPlugin.cs
File metadata and controls
101 lines (90 loc) · 4.05 KB
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
using System;
using Serilog;
using Sitecore.Framework.Messaging;
using Sitecore.XConnect;
using Sitecore.XConnect.Operations;
using Sitecore.XConnect.Service.Plugins;
namespace Sitecore.XConnect.ServicePlugins.InteractionsTracker.Plugins
{
public class ContactTrackerPlugin : IXConnectServicePlugin, IDisposable
{
private XdbContextConfiguration _config;
public ContactTrackerPlugin()
{
Log.Information("Create {0}", nameof(ContactTrackerPlugin));
}
/// <summary>Subscribes to events the current plugin listens to.</summary>
/// <param name="config">
/// A <see cref="T:Sitecore.XConnect.XdbContextConfiguration" /> object that provides access to the configuration settings.
/// </param>
/// <exception cref="T:System.ArgumentNullException">
/// Argument <paramref name="config" /> is a <b>null</b> reference.
/// </exception>
public void Register(XdbContextConfiguration config)
{
Log.Information("Register {0}", nameof(ContactTrackerPlugin));
_config = config;
RegisterEvents();
}
/// <summary>
/// Unsubscribes from events the current plugin listens to.
/// </summary>
public void Unregister()
{
Log.Information("Unregister {0}", nameof(ContactTrackerPlugin));
UnregisterEvents();
}
private void RegisterEvents()
{
//Subscribe OperationCompleted event
_config.OperationCompleted += OnOperationCompleted;
}
private void UnregisterEvents()
{
//Unsubscribe OperationCompleted event
_config.OperationCompleted -= OnOperationCompleted;
}
/// <summary>
/// Handles the event that is generated when an operation completes.
/// </summary>
/// <param name="sender">The <see cref="T:System.Object" /> that generated the event.</param>
/// <param name="xdbOperationEventArgs">A <see cref="T:Sitecore.XConnect.Operations.XdbOperationEventArgs" /> object that provides information about the event.</param>
private void OnOperationCompleted(object sender, XdbOperationEventArgs xdbOperationEventArgs)
{
//Check if no exceptions are occurred during executing the operation. If it is, it will not guarantee that contact was created.
if (xdbOperationEventArgs.Operation.Exception != null)
return;
//We need to track only the AddContactOperation operation. Trying to cast to a necessary type.
var operation = xdbOperationEventArgs.Operation as AddContactOperation;
//Checking if it is the necessary operation and if an operation execution status is "Succeeded"
if ((operation?.Status == XdbOperationStatus.Succeeded && operation.Entity.Id.HasValue)
|| (operation?.Status == XdbOperationStatus.Succeeded))
{
Log.Information("Processing add contact creation interaction with contact id = {0}", operation.Entity.Id.Value);
//Sending a message with an id of newly created contact.
DataExportService.SendContactInteraction(Convert.ToString(operation.Entity.Id.Value));
}
}
/// <summary>
/// Releases managed and unmanaged resources used by the current class instance.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases managed and unmanaged resources used by the current class instance.
/// </summary>
/// <param name="disposing">
/// Indicates whether the current method was called from explicitly or implicitly during finalization.
/// </param>
protected virtual void Dispose(bool disposing)
{
if (!disposing)
return;
Log.Information("Dispose {0}", nameof(ContactTrackerPlugin));
_config = null;
}
}
}