-
Notifications
You must be signed in to change notification settings - Fork 3
/
tag-rocket-product-removal-tag-level.html
109 lines (84 loc) · 3.65 KB
/
tag-rocket-product-removal-tag-level.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
<script data-cfasync="false">
/*
Web Site Advantage: Tag Rocket Product Removal Tag Level [v1.0]
Remove products that contain a word in their name
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)};
// rule on when to exclude an item from Facebook or Pinterest
var excludeWord = 'free';
const excludeWordRegex = new RegExp('.*'+excludeWord+'.*', 'gi');
var excludeItem = function(item) {
return item.name.match(excludeWordRegex) != null;
};
// this removes items from all events that send items, but does not adjust things like an orders totals
T.init(function() {
// Facebook, remove certain products
var fbProductsToRemove = [];
T.on('GetValue',function(data, eventName){
if (data.destination == 'FacebookAds' && excludeItem(data.item)) {
fbProductsToRemove.push(data.value);
}
});
T.on('FbqCommandPending',function(data, eventName){
var args = data.arguments;
if(!fbProductsToRemove) return;
if(args.length < 3) return;
var event = args[1];
var options = args[2];
if (!options.content_ids && !options.contents) return;
if (options.content_ids) {
var content_ids = options.content_ids.filter(function(id) {
var remove = fbProductsToRemove.includes(id);
return !remove;
});
options.content_ids = content_ids;
}
if (options.contents) {
var contents = options.contents.filter(function(item) {
var remove = fbProductsToRemove.includes(item.id);
return !remove;
});
options.contents = contents;
}
if ((!options.content_ids || options.content_ids.length == 0) && (!options.contents || options.contents.length == 0)) {
data.cancel = true;
}
});
// Pinterest, remove certain products
var pinterestProductsToRemove = [];
T.on('GetValue',function(data, eventName){
if (data.destination == 'PinterestAds' && excludeItem(data.item)) {
pinterestProductsToRemove.push(data.value);
}
});
T.on('PintrkCommandPending',function(data, eventName){
var args = data.arguments;
if(!pinterestProductsToRemove) return;
if(args.length < 3) return;
var event = args[1];
var options = args[2];
if (!options.product_ids && !options.line_items) return;
if (options.product_ids) {
var product_ids = options.product_ids.filter(function(id) {
var remove = pinterestProductsToRemove.includes(id);
return !remove;
});
options.product_ids = product_ids;
}
if (options.line_items) {
var line_items = options.line_items.filter(function(item) {
var remove = pinterestProductsToRemove.includes(item.product_id);
return !remove;
});
options.line_items = line_items;
}
if ((!options.product_ids || options.product_ids.length == 0) && (!options.line_items || options.line_items.length == 0)) {
data.cancel = true;
}
});
})
}(window,"TagRocket")
</script>