How to Google Analytic Download File Tracking?
I create a field called Downloads and attached the code to the download completed event and incremented the download number every time the file was downloaded, I have attached a video of the behavior.
You can utilize the following code in the global.asax to hook to the event:
using
System;
using
System.Linq;
using
Telerik.Sitefinity.Abstractions;
using
Telerik.Sitefinity.Libraries.Model;
using
Telerik.Sitefinity.Model;
using
Telerik.Sitefinity.Modules.Libraries;
using
Telerik.Sitefinity.Modules.Libraries.Web.Events;
using
Telerik.Sitefinity.Services;
namespace
SitefinityWebApp
{
public
class
Global : System.Web.HttpApplication
{
protected
void
Application_Start(
object
sender, EventArgs e)
{
Bootstrapper.Initialized += Bootstrapper_Initialized;
}
void
Bootstrapper_Initialized(
object
sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
{
if
(e.CommandName ==
"Bootstrapped"
)
{
EventHub.Subscribe<IMediaContentDownloadedEvent>(evt => LibrariesEventHandler(evt));
}
}
public
void
LibrariesEventHandler(IMediaContentDownloadedEvent eventInfo)
{
var userId = eventInfo.UserId;
var itemUrl = eventInfo.Url;
var itemType = eventInfo.Type;
var itemTitle = eventInfo.Title;
var librariesProviderName = eventInfo.ProviderName;
var itemMIMEType = eventInfo.MimeType;
var libraryId = eventInfo.LibraryId;
var downloadRequestHeaders = eventInfo.Headers;
var itemId = eventInfo.FileId;
var manager = LibrariesManager.GetManager();
var item = manager.GetDocument(itemId);
var masterItem = manager.Lifecycle.GetMaster(item)
as
Document;
var downloads = (
decimal
)masterItem.GetValue(
"Downloads"
);
var newdownloads = downloads + 1;
masterItem.SetValue(
"Downloads"
, newdownloads);
manager.SaveChanges();
}
protected
void
Session_Start(
object
sender, EventArgs e)
{
}
protected
void
Application_BeginRequest(
object
sender, EventArgs e)
{
}
protected
void
Application_AuthenticateRequest(
object
sender, EventArgs e)
{
}
protected
void
Application_Error(
object
sender, EventArgs e)
{
}
protected
void
Session_End(
object
sender, EventArgs e)
{
}
protected
void
Application_End(
object
sender, EventArgs e)
{
}
}
}