Created
March 10, 2017 09:19
-
-
Save blacktambourine/730178e09bb9ed4c34b52900c4747a4c to your computer and use it in GitHub Desktop.
Sitecore Page Events to add Search Terms to Analytics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Sitecore.Analytics; | |
using Sitecore.Analytics.Data; | |
using Sitecore.Analytics.Tracking; | |
using Sitecore.Data; | |
namespace BlackTambourine.Core.Sitecore.Helpers | |
{ | |
public class PageEvents | |
{ | |
public static ID PageEventSearchId { get { return new ID("{0C179613-2073-41AB-992E-027D03D523BF}"); } } | |
public static ID PageEventDownloadId { get { return new ID("{FA72E131-3CFD-481C-8E15-04496E9586DC}"); } } | |
/// <summary> | |
/// Register a Search Keyword in Sitecore Experience Analytics (Behaviour > Internal Search) | |
/// </summary> | |
/// <param name="searchterm"></param> | |
/// <param name="itemId"></param> | |
public void RegisterSearchTerm(string searchterm, ID itemId) | |
{ | |
if (searchterm == null) | |
{ | |
return; | |
} | |
if (!TrackerEnabled()) | |
{ | |
return; | |
} | |
var page = CurrentPage(); | |
page.Register(new PageEventData("Search", PageEventSearchId.Guid) | |
{ | |
ItemId = itemId.ToGuid(), | |
Data = searchterm, | |
DataKey = searchterm, | |
Text = searchterm | |
}); | |
} | |
/// <summary> | |
/// Register a Media Library item download in Sitecore Experience Analytics (Behaviour > Assets > Downloads) | |
/// </summary> | |
/// <param name="downloadedResourceText"></param> | |
/// <param name="itemId"></param> | |
public void RegisterDownload(string downloadedResourceText, ID itemId) | |
{ | |
if (downloadedResourceText == null) | |
{ | |
return; | |
} | |
if (!TrackerEnabled()) | |
{ | |
return; | |
} | |
var page = CurrentPage(); | |
page.Register(new PageEventData("Download", PageEventDownloadId.Guid) | |
{ | |
ItemId = itemId.ToGuid(), | |
Data = downloadedResourceText, | |
DataKey = downloadedResourceText, | |
Text = "Resource Downloaded" | |
}); | |
} | |
/// <summary> | |
/// Register a Page Event | |
/// </summary> | |
/// <param name="pageEventId"></param> | |
/// <param name="pageEventName"></param> | |
/// <param name="itemId"></param> | |
/// <param name="text"></param> | |
/// <param name="data"></param> | |
public void RegisterPageEvent(ID pageEventId, string pageEventName, ID itemId, string text, string data) | |
{ | |
if (pageEventId.IsNull) | |
{ | |
return; | |
} | |
if (!TrackerEnabled()) | |
{ | |
return; | |
} | |
var page = CurrentPage(); | |
page.Register(new PageEventData(pageEventName, pageEventId.Guid) | |
{ | |
ItemId = itemId.ToGuid(), | |
Data = data, | |
DataKey = data, | |
Text = text | |
}); | |
} | |
private ICurrentPageContext CurrentPage() | |
{ | |
return Tracker.Current.Session.Interaction.CurrentPage; | |
} | |
private static bool TrackerEnabled() | |
{ | |
return Tracker.IsActive && Tracker.Current.Session != null && Tracker.Current.Session.Interaction != null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment