Skip to content

Commit 72acef7

Browse files
Implement GTM
1 parent e309554 commit 72acef7

File tree

5 files changed

+101
-38
lines changed

5 files changed

+101
-38
lines changed

BlazorUniversalAnalytics/BUA/BUAStrategy.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public async Task Initialize()
4242
{
4343
await module.InvokeAsync<string>("BUAInitializeGoogleAnalytics", _GAID);
4444
}
45+
46+
if (HasToStartGTM())
47+
{
48+
await module.InvokeAsync<string>("BUAInitializeGTM", _GTMID);
49+
}
4550
}
4651

4752
public async Task TrackNavigation(string uri)
@@ -76,21 +81,26 @@ public async Task TrackEventFacebookPixel(
7681
await module.InvokeAsync<string>("BUATrackEventsFacebookPixel", eventName, objectValue);
7782
}
7883

84+
public async Task TrackEventGTM(
85+
string eventName,
86+
object objectValue = null)
87+
{
88+
var module = await Module;
89+
await module.InvokeAsync<string>("BUATrackEventsGTM", eventName, objectValue);
90+
}
91+
7992
private bool HasToStartFacebookPixel()
8093
{
81-
if (!String.IsNullOrEmpty(_FBPID) && String.IsNullOrEmpty(_GTMID))
82-
{
83-
return true;
84-
}
85-
return false;
94+
return !String.IsNullOrEmpty(_FBPID);
8695
}
8796

8897
private bool HasToStartGoogleAnalytics()
8998
{
90-
if (!String.IsNullOrEmpty(_GAID) && String.IsNullOrEmpty(_GTMID))
91-
{
92-
return true;
93-
}
94-
return false;
99+
return !String.IsNullOrEmpty(_GAID);
100+
}
101+
102+
private bool HasToStartGTM()
103+
{
104+
return !String.IsNullOrEmpty(_GTMID);
95105
}
96106
}

BlazorUniversalAnalytics/BUA/IBUA.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ public interface IBUA
1111
Task TrackEventGtag(string eventName, object objectValue = null);
1212

1313
Task TrackEventFacebookPixel(string eventName, object objectValue = null);
14+
15+
Task TrackEventGTM(string eventName, object objectValue = null);
1416
}

BlazorUniversalAnalytics/wwwroot/blazor.universal.analytics.js

Lines changed: 71 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ export function BUAInitGlobals() {
55
function () {
66
dataLayer.push(arguments);
77
};
8+
window.isGA = false;
9+
window.isFBP = false;
10+
window.isGTM = false;
811
}
912

1013
export function BUAInitializeFacebookPixel(FBPID) {
@@ -32,6 +35,8 @@ export function BUAInitializeFacebookPixel(FBPID) {
3235
"https://connect.facebook.net/en_US/fbevents.js"
3336
);
3437
fbq("init", FBPID);
38+
fbq("track", "PageView");
39+
window.isFBP = true;
3540
}
3641

3742
export function BUAInitializeGoogleAnalytics(GAID) {
@@ -41,17 +46,31 @@ export function BUAInitializeGoogleAnalytics(GAID) {
4146
document.head.appendChild(script);
4247
gtag("js", new Date());
4348
gtag("config", GAID);
49+
window.isGA = true;
4450
}
4551

46-
export function BUATrackNavigation(url, GAID) {
47-
if (typeof fbq == "function") {
48-
fbq("track", "PageView");
49-
}
52+
export function BUAInitializeGTM(GTMID) {
53+
(function (w, d, s, l, i) {
54+
w[l] = w[l] || [];
55+
w[l].push({
56+
"gtm.start": new Date().getTime(),
57+
event: "gtm.js",
58+
});
59+
var f = d.getElementsByTagName("head")[0],
60+
j = d.createElement(s),
61+
dl = l != "dataLayer" ? "&l=" + l : "";
62+
j.async = true;
63+
j.src = "https://www.googletagmanager.com/gtm.js?id=" + i + dl;
64+
f.appendChild(j, f);
65+
dataLayer.push({ event: "pageview" });
66+
window.isGTM = true;
67+
})(window, document, "script", "dataLayer", GTMID);
68+
}
5069

51-
if (typeof gtag == "function") {
52-
var path = url.replace(window.location.origin, "");
53-
gtag("config", GAID, { page_path: path });
54-
}
70+
export function BUATrackNavigation(url, GAID) {
71+
GTMPageView(url);
72+
FacebookPixelPageView();
73+
GoogleAnalyticsPageView(url, GAID);
5574
}
5675

5776
export function BUATrackEventsGtag(
@@ -60,27 +79,55 @@ export function BUATrackEventsGtag(
6079
eventCategory,
6180
eventLabel
6281
) {
63-
gtag('event', eventName, {
64-
'event_category': eventCategory,
65-
'event_label': eventLabel,
66-
'value': eventValue,
82+
gtag("event", eventName, {
83+
event_category: eventCategory,
84+
event_label: eventLabel,
85+
value: eventValue,
6786
});
6887
}
6988

70-
export function BUATrackEventsGtagByObjectValue(
71-
eventName,
72-
objectValue
73-
) {
74-
gtag('event', eventName, objectValue);
89+
export function BUATrackEventsGtagByObjectValue(eventName, objectValue) {
90+
gtag("event", eventName, objectValue);
7591
}
7692

77-
export function BUATrackEventsFacebookPixel(
78-
eventName,
79-
objectValue
80-
) {
81-
if (typeof objectValue === 'object' && objectValue !== null) {
82-
fbq('track', eventName, objectValue);
93+
export function BUATrackEventsFacebookPixel(eventName, objectValue) {
94+
if (typeof objectValue === "object" && objectValue !== null) {
95+
fbq("track", eventName, objectValue);
8396
} else {
84-
fbq('track', eventName);
97+
fbq("track", eventName);
98+
}
99+
}
100+
101+
export function BUATrackEventsGTM(eventName, objectValue) {
102+
if (eventName !== null && objectValue !== null) {
103+
objectValue['event'] = eventName;
104+
dataLayer.push(objectValue);
105+
}
106+
}
107+
108+
function GetPath(url) {
109+
return url.replace(window.location.origin, "");
110+
}
111+
112+
function FacebookPixelPageView(callFromGTM = false) {
113+
if ((window.isFBP || callFromGTM) && typeof fbq == "function") {
114+
fbq("track", "PageView");
115+
}
116+
}
117+
118+
function GoogleAnalyticsPageView(url, GAID, callFromGTM) {
119+
if ((window.isGA || callFromGTM) && typeof gtag == "function") {
120+
gtag("config", GAID, { page_path: GetPath(url) });
121+
}
122+
}
123+
124+
function GTMPageView(url) {
125+
if (window.isGTM) {
126+
dataLayer.push({
127+
event: "pageview",
128+
page: {
129+
path: GetPath(url)
130+
}
131+
});
85132
}
86133
}

Demo/Client/Pages/Counter.razor

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
{
2525
currentCount++;
2626

27-
Analytics.TrackEventGtag("event-name", "event-value", "event-category", "event-label");
27+
//Analytics.TrackEventGtag("event-name", "event-value", "event-category", "event-label");
2828
2929
// IMPORTANT: The object SampleDate are used below just as an example.
3030
// You must to check the correct object properties on respective Analytic tool you are using.
3131
32-
Analytics.TrackEventGtag("event-name", SampleData);
33-
Analytics.TrackEventFacebookPixel("event-name", SampleData);
32+
//Analytics.TrackEventGtag("event-name", SampleData);
33+
//Analytics.TrackEventFacebookPixel("event-name", SampleData);
34+
Analytics.TrackEventGTM("event-name", SampleData);
3435
}
3536
}

Demo/Client/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ public static async Task Main(string[] args)
1515

1616
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
1717

18-
builder.Services.AddBUA("YOUR_GTAG_ID", "YOUR_FBPIXEL_ID", "YOUR_GTM_ID");
18+
19+
// Parameters (GAID, FACEBOOKPIXELID, GTMID)
20+
//builder.Services.AddBUA("UA-XXXXXXXX-X", "XXXXXXXXXXXXXXX", null);
21+
builder.Services.AddBUA(null, null, "GTM-XXXXXXX");
1922

2023
await builder.Build().RunAsync();
2124
}

0 commit comments

Comments
 (0)