You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Become a Sponsor to support my work: <a href="https://github.com/sponsors/thyngster">GITHUB Sponsor</a>
18
-
</p>
18
+
</p>
19
+
20
+
# Why this library
19
21
20
-
# Why this library
21
22
One of the best additions to Universal Analytics in the past was the Tasks, more specifically the customTask that allowed us to modify the hits payloads before theywere sent to the endpoint.
22
23
23
24
In April 2024, Google Switched the use of sendBeacon to the fetch API. ( not a breaking change since sendBeacon seems to work on top of Fetch ), and along with the chance
@@ -33,57 +34,48 @@ The current features are:
33
34
- Allows callbacks concatenations
34
35
- Measurement ID based setup ( apply the callbacks only to the defined hits )
35
36
36
-
37
-
38
37
Not only this, I tool some time to port and improve all the customTask I was able to find around on internet so you can just use them on your setup.
39
38
40
-
41
-
42
39
# How to Use It
43
40
44
-
45
-
46
41
One we have loaded the GA4CustomTask.js code we need to instanciate our Tasker:
47
42
48
-
49
-
50
43
```
51
44
var ga4CustomTaskInterceptor = new GA4CustomTask({
52
-
allowedMeasurementIds: ["G-DEBUGEMALL"],
53
-
tasks: [
54
-
logRequestsToConsoleTask,
55
-
task2,
56
-
task3
57
-
]
45
+
allowedMeasurementIds: ["G-DEBUGEMALL"],
46
+
tasks: [
47
+
logRequestsToConsoleTask,
48
+
task2,
49
+
task3
50
+
]
58
51
});
59
-
```
52
+
```
53
+
60
54
Since this is shared between all instances in your pages, you need to specify at least one allowedMeasurementIds, this way only these requests will be intercepted.
61
55
62
-
Also, if there's no tasks nothing will be intercepted.
56
+
Also, if there's no tasks nothing will be intercepted.
63
57
64
58
# Building your own Task
65
59
66
60
You can create your own custom tasks. By default, the library provides a `RequestModel` interface, which includes `sharedPayload` and `events` in object format. The library handles:
67
61
68
-
69
62
- Parsing the request type (GET/POST)
70
63
- Managing multiple events
71
-
- Constructing the final fetch request for your convenience
64
+
- Constructing the final fetch request for your convenience
72
65
73
66
```
74
67
interface RequestModel {
75
-
endpoint: string;
76
-
sharedPayload: { [key: string]: any }; // No need for null in the type
77
-
events: { [key: string]: any }[];
78
-
__skip?: boolean;
68
+
endpoint: string;
69
+
sharedPayload: { [key: string]: any }; // No need for null in the type
0 commit comments