Skip to content

Commit

Permalink
Add support for more generic events #33 (#34)
Browse files Browse the repository at this point in the history
* Make it possible to track custom event

* Update ReadMe with new event tracking.
  • Loading branch information
ErikHen authored May 12, 2021
1 parent e0b4676 commit 1c27223
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 13 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ Inside your main `Startup`/`Program`, call `AddGoogleAnalytics`. This will confi
# How to trigger an Analytics Event

1. Inject `IAnalytics` wherever you want to trigger the event.
2. Call `IAnalytics.TrackEvent` passing the `EventName`, `Value` and `Category` (optional).
2. Call `IAnalytics.TrackEvent` passing the `EventName` and `EventData` (an object containing the event data).
<br>Or<br>
Call `IAnalytics.TrackEvent` passing the `EventName`, `Value` and `Category` (optional).

```
@inject Blazor.Analytics.IAnalytics Analytics
Analytics.TrackEvent("generate_lead", new {currency = "USD", value = 99.99});
```

# Changelog
### v3.1.0
Expand Down
3 changes: 3 additions & 0 deletions demo/DemoApp/DemoApp.Client/Pages/Counter.razor
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
{
currentCount++;
Analytics.TrackEvent("Increment", currentCount, "CountPage");

//Example of how to track a generic event (see also https://developers.google.com/gtagjs/reference/ga4-events)
Analytics.TrackEvent("generate_lead", new {currency = "USD", value = 99.99});
}
}
5 changes: 4 additions & 1 deletion demo/DemoApp/DemoApp.Server/Pages/Counter.razor
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
private void IncrementCount()
{
currentCount++;
Analytics.TrackEvent("Increment", currentCount, "CountPage");
//Analytics.TrackEvent("Increment", currentCount, "CountPage");
//Example of how to track a generic event (see also https://developers.google.com/gtagjs/reference/ga4-events)
Analytics.TrackEvent("generate_lead", new {currency = "USD", value = currentCount});
}
}
1 change: 1 addition & 0 deletions src/Blazor.Analytics/Abstractions/IAnalytics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public interface IAnalytics

Task TrackEvent(string eventName, string eventCategory = null, string eventLabel = null, int? eventValue = null);
Task TrackEvent(string eventName, int eventValue, string eventCategory = null, string eventLabel = null);
Task TrackEvent(string eventName, object eventData);
}
}
2 changes: 1 addition & 1 deletion src/Blazor.Analytics/Blazor.Analytics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

<!-- TypeScript Build -->
<ItemGroup>
<PackageReference Include="Microsoft.TypeScript.MSBuild" Version="4.0.2">
<PackageReference Include="Microsoft.TypeScript.MSBuild" Version="4.2.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
22 changes: 16 additions & 6 deletions src/Blazor.Analytics/GoogleAnalytics/GoogleAnalyticsStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ public async Task TrackEvent(
string eventCategory = null,
string eventLabel = null,
int? eventValue = null)
{
await TrackEvent(eventName, new
{
event_category = eventCategory,
event_label = eventLabel,
value = eventValue
});
}

public Task TrackEvent(string eventName, int eventValue, string eventCategory = null, string eventLabel = null)
{
return TrackEvent (eventName, eventCategory, eventLabel, eventValue);
}

public async Task TrackEvent(string eventName, object eventData)
{
if (!_isInitialized)
{
Expand All @@ -63,12 +78,7 @@ public async Task TrackEvent(

await _jsRuntime.InvokeAsync<string>(
GoogleAnalyticsInterop.TrackEvent,
eventName, eventCategory, eventLabel, eventValue);
}

public Task TrackEvent(string eventName, int eventValue, string eventCategory = null, string eventLabel = null)
{
return TrackEvent (eventName, eventCategory, eventLabel, eventValue);
eventName, eventData);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ namespace GoogleAnalyticsInterop
}
}

export function trackEvent(eventName: string, eventCategory: string, eventLabel: string, eventValue: string)
export function trackEvent(eventName: string, eventData: object)
{
gtag("event", eventName, { event_category: eventCategory, event_label: eventLabel, value: eventValue });
if(this.debug){
console.log(`[GTAG][Event triggered]: ${eventName}`);
gtag("event", eventName, eventData);
if (this.debug) {
console.log(`[GTAG][Event triggered]: ${eventName}`);
}
}
}

0 comments on commit 1c27223

Please sign in to comment.