-
Notifications
You must be signed in to change notification settings - Fork 3
/
tag-rocket-ga4-client_id_to_dataLayer.html
40 lines (32 loc) · 1.35 KB
/
tag-rocket-ga4-client_id_to_dataLayer.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<script data-cfasync="false">
/*
Web Site Advantage: Tag Rocket GA4 client_id to dataLayer [v1.1]
Sends a dataLayer event with the client_id
https://bigcommerce.websiteadvantage.com.au/tag-rocket/articles/tag-rocket-api/
Copyright (C) 2023 Web Site Advantage
*/
!function(w,t){
w[t]=w[t]||{};var T=w[t];T.i=T.i||[];if(!T.init)T.init=function(f){T.i.push(f)};
T.init(function() {
T.on('GtagCommandPending',function(data, eventName){
if (data.arguments[0] === 'config' && data.arguments.length >= 3) {
var id = data.arguments[1];
if (id.indexOf('G-') == 0) {
var options = data.arguments[2];
// Following Simo's pattern: https://github.com/gtm-templates-simo-ahava/gtag-get-api/blob/main/template.tpl
// more complex is want to get multiple values
gtag('get', id, 'client_id', function(client_id) {
dataLayer.push({
event: 'gtagApiGet',
gtagApiResult: {
id: id,
client_id: client_id
}
});
})
}
};
});
})
}(window,"TagRocket")
</script>