-
Notifications
You must be signed in to change notification settings - Fork 3
/
tag-rocket-reddit-ads.html
178 lines (148 loc) · 6.06 KB
/
tag-rocket-reddit-ads.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<script data-cfasync="false">
/*
Web Site Advantage: Tag Rocket Reddit Ads [v1.0]
Base script plus ecommerce events
https://bigcommerce.websiteadvantage.com.au/tag-rocket/articles/tag-rocket-api/
Copyright (C) 2023 Web Site Advantage
*/
!function(w,t){
var advertiserId = 'advertiser-id';
var itemGroupIdFormat = "[[psku]]||P[[pid]]";
var enableFirstPartyCookies = true;
var useDecimalCurrencyValues = true;
var debug = false;
// End of settings --------
// links to documentation
// https://reddit.my.site.com/helpcenter/s/article/Install-the-Reddit-Pixel-on-your-website - includes a list of standard events
// https://reddit.my.site.com/helpcenter/s/article/Reddit-Pixel-Event-Metadata - parameters for the events
// https://reddit.my.site.com/helpcenter/s/article/Reddit-Pixel-Advanced-Matching
// https://reddit.my.site.com/helpcenter/s/article/Reddit-Pixel-Custom-Events - example is a promotion view
// https://reddit.my.site.com/helpcenter/s/article/Reddit-Ads-Pixel-Retargeting
// https://reddit.my.site.com/helpcenter/s/article/Verify-the-Reddit-Pixel-is-working-correctly
// https://reddit.my.site.com/helpcenter/s/article/Events-Manager
var tagName = 'Reddit';
var consentRequired = 'targetingAdvertising';
var getProductId = function(item) {
return T.getProductId(tagName, 'ItemGroupId', item, itemGroupIdFormat);
};
var productInformation = function(item, change) {
var product = {
id: getProductId(item),
name: item.name
};
if (item.category)
product.category = item.category.name || item.category;
else
product.category = '-No Category-'; // as it is required.
return product;
}
var cartInformation = function(cart) {
var tagData = {
value: cart.orderAmount,
currency: cart.currency.code,
itemCount: 0,
products: []
};
if (cart.orderId) tagData.transactionId = '' + cart.orderId;
for(var i =0; i < cart.items.length; i++) {
var item = cart.items[i];
tagData.itemCount += item.quantity;
tagData.products.push(productInformation(item));
}
return tagData;
};
if(!w.rdt){
var p=w.rdt=function(){
p.sendEvent?p.sendEvent.apply(p,arguments):p.callQueue.push(arguments) // directly send if loaded, else queue it up
};
p.callQueue=[];
}
var CallTag = function(arg1) {
if(debug) console.log(tagName, arguments);
w.rdt.apply(w.rdt, arguments);
}
// Establish the TagRocket tag
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() {
var initOptions = {
// optOut: false, // default. opt out of retargetting
integration: "tagrocket",
useDecimalCurrencyValues: useDecimalCurrencyValues // GTM template is always true, so is suggested code
};
{{#if customer.email}}
initOptions.email = {{{json (trim customer.email)}}}.toLowerCase();
{{/if}}
{{#if customer.id}}
initOptions.externalId = '{{customer.id}}';
{{/if}}
CallTag('init', advertiserId, initOptions);
if (!enableFirstPartyCookies) {
CallTag('disableFirstPartyCookies');
}
CallTag('track', 'PageVisit');
var scriptTagAdded = false;
function addScriptTag() {
if (!scriptTagAdded) {
scriptTagAdded = true;
T.addPreconnectTag('https://www.redditstatic.com');
T.addScriptTag('https://www.redditstatic.com/ads/pixel.js');
}
}
if (T.consent[consentRequired]) {
addScriptTag();
}
else {
T.on('ConsentChanged',function(data){
if (T.consent[consentRequired]) addScriptTag();
});
}
T.on('.*',function(eventData, eventName){
switch(eventName) {
case "ProductPage":
CallTag('track', 'ViewContent', {
products: [productInformation(eventData)]
});
break;
case "ProductsVisible":
var tagData = {
products: eventData.list.items.map(function(item){
return productInformation(item);
})
}
CallTag('track', eventData.search ? 'Search' : 'ViewContent', tagData);
break;
case "CartItemChanged":
if(eventData.change > 0) {
CallTag('track', 'AddToCart', {
itemCount: eventData.change,
currency: eventData.currency,
value: eventData.price * eventData.change,
products: [productInformation(eventData.item, eventData.change)]
});
}
break;
case "CheckoutStep5OrderCompleted":
CallTag('track', 'Purchase', cartInformation(eventData));
break;
case "AccountCreated":
CallTag('track', 'SignUp');
break;
case "ContactSuccess":
CallTag('track', 'Lead');
break;
case "SubscribeSuccess":
CallTag('track', 'Lead');
break;
case "AddToWishList":
CallTag('track', 'AddToWishlist', {
itemCount: 1,
currency: eventData.currency,
value: eventData.price,
products: [productInformation(eventData)]
});
break;
}
});
})
}(window,"TagRocket")
</script>