How to track a logged in user email when a download trigger event occurs in Sitecore website

132 views Asked by At

I would like to track a logged in user email when the user downloads PDF extension of a media item.

Currently I can track downloads, but not their logged in user email. Please let me know how to customise this programmatically in Sitecore 9.

1

There are 1 answers

0
Kate Orlova On

You can add a download event to a user interaction by using the client.AddInteraction() extension method. For example, the below code adds a new interaction with a download event to an existing contact.

using Sitecore.XConnect.Client;
using Sitecore.XConnect.Collection.Model;
using Sitecore.XConnect;
using System;

...

using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
 {
     try
     {
         var existingContact = client.Get<Sitecore.XConnect.Contact>(new Sitecore.XConnect.IdentifiedContactReference("source", "identifier"), new Sitecore.XConnect.ContactExpandOptions());

         if (existingContact != null)
         {
            Guid mediaItemId = Guid.NewGuid(); // Replace with real media item ID GUI
            Guid channelId = Guid.NewGuid(); // Replace with real channel ID GUID
            string userAgent = "Sample User Agent";

            // Interaction
            var interaction = new Interaction(existingContact, InteractionInitiator.Brand, channelId, userAgent);

            Sitecore.XConnect.Collection.Model.DownloadEvent downloadEvent = new DownloadEvent(DateTime.UtcNow, mediaItemId)
            {
              Text = "Some PDF Asset" // Not mandatory
            };

            interaction.Events.Add(downloadEvent);
            client.AddInteraction(interaction);
            client.Submit();
         }
     }
     catch (XdbExecutionException ex)
     {
         // Handle exception
     }
 }