question

azeempak-0292 avatar image
1 Vote"
azeempak-0292 asked DanielZhang-MSFT edited

Implementing a tracking pixel in Blazor Server

I'm trying to implement a tracking pixel in Blazor to track the open rate of a newsletter list. To do so I thought that I had to work on circuits so I realized a little circuit counter to investigate this possibility:

 public class CircuitCounter : CircuitHandler, ICircuitCounter
 { 
     private long circuitCount = 0;
    
      
     public override Task OnCircuitOpenedAsync(Circuit circuit, CancellationToken cancellationToken)
     {
         long updatedCircuitCount = Interlocked.Increment(ref circuitCount);
         NotifyUpdatedCircuitCount(updatedCircuitCount);
         return base.OnCircuitOpenedAsync(circuit, cancellationToken);
     }
    
     private void NotifyUpdatedCircuitCount(long updatedCircuitCount)
     {
         CircuitCountChanged?.Invoke(this, updatedCircuitCount);
     }
    
     public long CurrentCircuitCount => Interlocked.Read(ref circuitCount);
     public event EventHandler<long> CircuitCountChanged;
 }

View of the counter:


 @page "/counter"
 @inject CircuitHandlers.ICircuitCounter circuitCounter
 @implements IDisposable
 <h1>Circuit counter</h1>
 <p>Circuits opened: @currentCircuitCount</p>
    
 @code {
     long currentCircuitCount;
     protected override void OnInitialized()
     {
         currentCircuitCount = circuitCounter.CurrentCircuitCount;
         circuitCounter.CircuitCountChanged += UpdateCircuitCount;
     }
     private void UpdateCircuitCount(object sender, long circuitCount)
     {
         InvokeAsync(() =>
         {
     currentCircuitCount = circuitCount;
             StateHasChanged();
         });
     }
     public void Dispose()
     {
         circuitCounter.CircuitCountChanged -= UpdateCircuitCount;
     }
 }

The problem is that the counter doesn't go up if I try to send myself an email containing a tracking pixel that looks like this

<img src="localhost:44334/foo" />

My guess at this point is that the attempt to fetch the image from that URL isn't enough to trigger the opening of a new circuit but if that's actually the case I'm a bit lost on how should I proceed instead and I wasn't really able to find anything Blazor related on the internet about this.


dotnet-csharpdotnet-aspnet-core-general
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

A tracking pixel triggers a database insert by calling an image handler or action. Your Blazor page simply queries the database when the page opens. If you want to see real time tracking the craft a timed loop in the same page.

0 Votes 0 ·

0 Answers