Skip to content

Commit

Permalink
Enabling support for tracking Video Watch Time
Browse files Browse the repository at this point in the history
  • Loading branch information
efonsecab committed Apr 11, 2024
1 parent 504278e commit df4bf72
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public partial class VideoWatchTime

public DateTimeOffset SessionStartDatetime { get; set; }

public int WatchTime { get; set; }
public double WatchTime { get; set; }

[StringLength(450)]
public string WatchedByApplicationUserId { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ public class VideoInfoModel : IListModel
public string[]? VideoKeywords { get; set; }
public string[]? VideoTopics { get; set; }
public string? EnglishCaptions { get; set; }
public int LifetimeSessions { get; set; }
public TimeSpan LifetimeWatchTime { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class VideoWatchTimeModel: IListModel
public Guid? SessionId { get; set; }= Guid.Empty;
[Required]
public DateTimeOffset? SessionStartDatetime { get; set; }
public int WatchTime { get; set; }
public double WatchTime { get; set; }
[StringLength(450)]
public string? WatchedByApplicationUserId { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ CancellationToken cancellationToken
VideoLanguageCode = p.VideoLanguageCode,
VideoVisibilityId = p.VideoVisibilityId,
ThumbnailUrl = p.ThumbnailUrl,
YouTubeVideoId = p.YouTubeVideoId
YouTubeVideoId = p.YouTubeVideoId,
LifetimeSessions = p.VideoWatchTime.Count,
LifetimeWatchTime = TimeSpan.FromSeconds(p.VideoWatchTime.Sum(p=>p.WatchTime))
});
if (!String.IsNullOrEmpty(orderByString))
query = query.OrderBy(orderByString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[VideoInfoId] BIGINT NOT NULL,
[SessionId] UNIQUEIDENTIFIER NOT NULL,
[SessionStartDatetime] DATETIMEOFFSET NOT NULL,
[WatchTime] INT NOT NULL,
[WatchTime] FLOAT NOT NULL,
[WatchedByApplicationUserId] NVARCHAR(450) NULL,
CONSTRAINT [FK_VideoWatchTime_VideoInfo] FOREIGN KEY ([VideoInfoId]) REFERENCES [FairPlayTube].[VideoInfo]([VideoInfoId]),
CONSTRAINT [FK_VideoWatchTime_AspNetUsers] FOREIGN KEY ([WatchedByApplicationUserId]) REFERENCES [dbo].[AspNetUsers]([Id])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
</div>
<div>
<FluentLabel Typo="Typography.Body">@singleItem.Name</FluentLabel>
<FluentLabel Typo="Typography.Body">Views: @singleItem.LifetimeSessions</FluentLabel>
<FluentLabel Typo="Typography.Body">Watch Time(hh:mm:ss): @singleItem.LifetimeWatchTime</FluentLabel>
</div>
<div>
<FluentButton Type="ButtonType.Button" Appearance="Appearance.Accent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ else
private string? VideoUrl { get; set; }
private SupportedLanguageModel[]? SupportedLanguages { get; set; }
private DotNetObjectReference<WatchVideo>? dotNetObjectReference;

private bool IsSessionCreated { get; set; } = false;
protected override void OnInitialized()
{
this.dotNetObjectReference = DotNetObjectReference.Create<WatchVideo>(this);
Expand Down Expand Up @@ -92,13 +92,13 @@ else
}

[JSInvokable]
public async void UpdateWatchTime(int watchTime, string currentSessionGuid)
public async void UpdateWatchTime(double watchTime, string currentSessionGuid)
{
try
{
var currentUserId = this.userProviderService.GetCurrentUserId();
System.Diagnostics.Debug.WriteLine($"Watch Time (s): {watchTime}. Session: {currentSessionGuid}");
if (watchTime == 1)
if (!IsSessionCreated)
{
await this.videoWatchTimeService.CreateVideoWatchTimeAsync(
this.VideoId!,
Expand All @@ -108,8 +108,9 @@ else
SessionId = Guid.Parse(currentSessionGuid),
WatchTime = watchTime
}, cancellationToken: cancellationTokenSource.Token);
this.IsSessionCreated = true;
}
else if (watchTime % 30 == 0)
else
{
await this.videoWatchTimeService.UpdateVideoWatchTimeAsync(
this.VideoId!,
Expand Down

0 comments on commit df4bf72

Please sign in to comment.