@@ -17,8 +17,6 @@ public function __construct() {
17
17
$ this ->init_form_fields ();
18
18
$ this ->init_settings ();
19
19
20
- add_action ( 'wp_head ' , array ( $ this , 'add_gtag_script ' ) );
21
-
22
20
add_action ( 'eduadmin-list-course-view ' , array ( $ this , 'track_list_course_view ' ) );
23
21
add_action ( 'eduadmin-list-event-view ' , array ( $ this , 'track_list_event_view ' ) );
24
22
add_action ( 'eduadmin-detail-view ' , array ( $ this , 'track_detail_view ' ) );
@@ -36,82 +34,20 @@ public function __construct() {
36
34
*/
37
35
public function init_form_fields () {
38
36
$ this ->setting_fields = [
39
- 'enabled ' => [
37
+ 'enabled ' => [
40
38
'title ' => __ ( 'Enabled ' , 'eduadmin-analytics ' ),
41
39
'type ' => 'checkbox ' ,
42
40
'description ' => __ ( 'Enable Google Analytics / Tag Manager integration ' , 'eduadmin-analytics ' ),
43
41
'default ' => 'no ' ,
44
42
],
45
- 'google-tag-manager ' => [
46
- 'title ' => __ ( 'Google Tag Manager ID ' , 'eduadmin-analytics ' ),
47
- 'type ' => 'text ' ,
48
- 'description ' => __ ( 'The ID of the Google Tag Manager ' , 'eduadmin-analytics ' ),
49
- 'default ' => '' ,
50
- ],
51
- 'google-tag ' => [
52
- 'title ' => __ ( 'Google Tag ID ' , 'eduadmin-analytics ' ),
53
- 'type ' => 'text ' ,
54
- 'description ' => __ ( 'The ID of the Google Tag ' , 'eduadmin-analytics ' ),
55
- 'default ' => '' ,
56
- ],
57
43
];
58
44
}
59
45
60
- public function add_gtag_script () {
61
- if ( 'no ' === $ this ->get_option ( 'enabled ' , 'no ' ) ) {
62
- return ;
63
- }
64
-
65
- if ( ! empty ( $ this ->get_option ( 'google-tag-manager ' ) ) ) {
66
- ?>
67
- <!-- Google Tag Manager -->
68
- <script>(function (w, d, s, l, i) {
69
- w[l] = w[l] || [];
70
- w[l].push({
71
- 'gtm.start':
72
- new Date().getTime(), event: 'gtm.js'
73
- });
74
- var f = d.getElementsByTagName(s)[0],
75
- j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : '';
76
- j.async = true;
77
- j.src =
78
- 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
79
- f.parentNode.insertBefore(j, f);
80
- })(window, document, 'script', 'eduAdminDataLayer', '<?php echo esc_js ( $ this ->get_option ( 'google-tag-manager ' ) ); ?> ');</script>
81
- <!-- End Google Tag Manager -->
82
- <?php
83
- }
84
-
85
- if ( ! empty ( $ this ->get_option ( 'google-tag ' ) ) ) {
86
- $ currency = EDU ()->get_option ( 'eduadmin-currency ' , 'SEK ' );
87
- ?>
88
- <script async
89
- src="https://www.googletagmanager.com/gtag/js?id=<?php echo esc_attr ( $ this ->get_option ( 'google-tag ' ) ); ?> &l=eduAdminDataLayer"></script>
90
- <script>
91
- window.eduAdminDataLayer = window.eduAdminDataLayer || [];
92
-
93
- function gtag() {
94
- eduAdminDataLayer.push(arguments);
95
- }
96
-
97
- gtag('js', new Date());
98
- gtag('config', '<?php echo esc_js ( $ this ->get_option ( 'google-tag ' ) ); ?> ', {
99
- 'currency': '<?php echo esc_js ( $ currency ); ?> ',
100
- });
101
- </script>
102
- <?php
103
- }
104
- }
105
-
106
46
public function is_configured_and_enabled () {
107
47
if ( 'no ' === $ this ->get_option ( 'enabled ' , 'no ' ) ) {
108
48
return false ;
109
49
}
110
50
111
- if ( empty ( $ this ->get_option ( 'google-tag ' ) ) && empty ( $ this ->get_option ( 'google-tag-manager ' ) ) ) {
112
- return false ;
113
- }
114
-
115
51
return true ;
116
52
}
117
53
@@ -128,15 +64,27 @@ public function track_list_course_view( $courses ) {
128
64
'item_name ' => $ course ["CourseName " ],
129
65
];
130
66
67
+ if ( isset ( $ course ['Events ' ] ) ) {
68
+ foreach ( $ course ['Events ' ] as $ event ) {
69
+ $ course_item = $ this ->getItemsPriced ( $ event ['PriceNames ' ], $ course_item );
70
+ }
71
+ }
72
+
73
+ if ( ! isset ( $ course_item ['price ' ] ) ) {
74
+ $ course_item = $ this ->getItemsPriced ( $ course ['PriceNames ' ], $ course_item );
75
+ }
131
76
$ gtag_items = $ this ->getItemsCategorized ( $ course ["Categories " ], $ course_item , $ gtag_items );
132
77
}
133
78
134
79
if ( count ( $ gtag_items ) > 0 ) {
135
80
?>
136
- <script type="text/javascript">gtag('event', 'view_item_list', {
137
- 'item_list_id': 'course_list',
138
- 'item_list_name': 'Course list',
139
- 'items': <?php echo wp_json_encode ( $ gtag_items , JSON_PRETTY_PRINT ); ?> });</script>
81
+ <script type="text/javascript">if (gtag) {
82
+ gtag('event', 'view_item_list', {
83
+ 'item_list_id': 'course_list',
84
+ 'item_list_name': 'Course list',
85
+ 'items': <?php echo wp_json_encode ( $ gtag_items , JSON_PRETTY_PRINT ); ?> });
86
+ }
87
+ </script>
140
88
<?php
141
89
}
142
90
}
@@ -147,21 +95,30 @@ public function track_list_event_view( $events ) {
147
95
}
148
96
149
97
$ gtag_items = [];
98
+
150
99
foreach ( $ events as $ course ) {
151
100
$ course_item = [
152
101
'item_id ' => 'EV_ ' . $ course ["CourseTemplateId " ],
153
102
'item_name ' => $ course ["CourseName " ],
154
103
];
155
104
105
+ $ course_item = $ this ->getItemsPriced ( $ course ['PriceNames ' ], $ course_item );
106
+
107
+ if ( ! isset ( $ course_item ['price ' ] ) ) {
108
+ $ course_item = $ this ->getItemsPriced ( $ course ['CourseTemplate ' ]['PriceNames ' ], $ course_item );
109
+ }
110
+
156
111
$ gtag_items = $ this ->getItemsCategorized ( $ course ["Categories " ], $ course_item , $ gtag_items );
157
112
}
158
113
159
114
if ( count ( $ gtag_items ) > 0 ) {
160
115
?>
161
- <script type="text/javascript">gtag('event', 'view_item_list', {
162
- 'item_list_id': 'event_list',
163
- 'item_list_name': 'Event list',
164
- 'items': <?php echo wp_json_encode ( $ gtag_items , JSON_PRETTY_PRINT ); ?> });</script>
116
+ <script type="text/javascript">if (gtag) {
117
+ gtag('event', 'view_item_list', {
118
+ 'item_list_id': 'event_list',
119
+ 'item_list_name': 'Event list',
120
+ 'items': <?php echo wp_json_encode ( $ gtag_items , JSON_PRETTY_PRINT ); ?> });
121
+ }</script>
165
122
<?php
166
123
}
167
124
}
@@ -171,19 +128,32 @@ public function track_detail_view( $course_template ) {
171
128
return ;
172
129
}
173
130
131
+ $ currency = EDU ()->get_option ( 'eduadmin-currency ' , 'SEK ' );
132
+
174
133
$ gtag_items = [];
175
134
176
135
$ course_item = [
177
136
'item_id ' => 'CTI_ ' . $ course_template ["CourseTemplateId " ],
178
137
'item_name ' => $ course_template ["CourseName " ],
179
138
];
180
139
140
+ foreach ( $ course_template ['Events ' ] as $ event ) {
141
+ $ course_item = $ this ->getItemsPriced ( $ event ['PriceNames ' ], $ course_item );
142
+ }
143
+
144
+ if ( ! isset ( $ course_item ['price ' ] ) ) {
145
+ $ course_item = $ this ->getItemsPriced ( $ course_template ['PriceNames ' ], $ course_item );
146
+ }
147
+
181
148
$ gtag_items = $ this ->getItemsCategorized ( $ course_template ["Categories " ], $ course_item , $ gtag_items );
182
149
183
150
if ( count ( $ gtag_items ) > 0 ) {
184
151
?>
185
- <script type="text/javascript">gtag('event', 'view_item', {
186
- 'items': <?php echo wp_json_encode ( $ gtag_items , JSON_PRETTY_PRINT ); ?> });</script>
152
+ <script type="text/javascript">if (gtag) {
153
+ gtag('event', 'view_item', {
154
+ 'currency': '<?php echo esc_js ( $ currency ); ?> ',
155
+ 'items': <?php echo wp_json_encode ( $ gtag_items , JSON_PRETTY_PRINT ); ?> });
156
+ }</script>
187
157
<?php
188
158
}
189
159
}
@@ -193,17 +163,28 @@ public function track_programme_detail_view( $programme ) {
193
163
return ;
194
164
}
195
165
196
- $ gtag_items = [
197
- [
198
- 'item_id ' => 'PI_ ' . $ programme ["ProgrammeId " ],
199
- 'item_name ' => $ programme ["ProgrammeName " ],
200
- ]
166
+ $ currency = EDU ()->get_option ( 'eduadmin-currency ' , 'SEK ' );
167
+
168
+ $ gtag_items = [];
169
+
170
+ $ course_item = [
171
+ 'item_id ' => 'PI_ ' . $ programme ["ProgrammeId " ],
172
+ 'item_name ' => $ programme ["ProgrammeName " ],
201
173
];
202
174
175
+ if ( ! isset ( $ course_item ['price ' ] ) ) {
176
+ $ course_item = $ this ->getItemsPriced ( $ programme ['PriceNames ' ], $ course_item );
177
+ }
178
+
179
+ $ gtag_items [] = $ course_item ;
180
+
203
181
if ( count ( $ gtag_items ) > 0 ) {
204
182
?>
205
- <script type="text/javascript">gtag('event', 'view_item', {
206
- 'items': <?php echo wp_json_encode ( $ gtag_items , JSON_PRETTY_PRINT ); ?> });</script>
183
+ <script type="text/javascript">if (gtag) {
184
+ gtag('event', 'view_item', {
185
+ 'currency': '<?php echo esc_js ( $ currency ); ?> ',
186
+ 'items': <?php echo wp_json_encode ( $ gtag_items , JSON_PRETTY_PRINT ); ?> });
187
+ }</script>
207
188
<?php
208
189
}
209
190
}
@@ -213,19 +194,32 @@ public function track_booking_view( $course_template ) {
213
194
return ;
214
195
}
215
196
197
+ $ currency = EDU ()->get_option ( 'eduadmin-currency ' , 'SEK ' );
198
+
216
199
$ gtag_items = [];
217
200
218
201
$ course_item = [
219
202
'item_id ' => 'CTI_ ' . $ course_template ["CourseTemplateId " ],
220
203
'item_name ' => $ course_template ["CourseName " ],
221
204
];
222
205
206
+ foreach ( $ course_template ['Events ' ] as $ event ) {
207
+ $ course_item = $ this ->getItemsPriced ( $ event ['PriceNames ' ], $ course_item );
208
+ }
209
+
210
+ if ( ! isset ( $ course_item ['price ' ] ) ) {
211
+ $ course_item = $ this ->getItemsPriced ( $ course_template ['PriceNames ' ], $ course_item );
212
+ }
213
+
223
214
$ gtag_items = $ this ->getItemsCategorized ( $ course_template ["Categories " ], $ course_item , $ gtag_items );
224
215
225
216
if ( count ( $ gtag_items ) > 0 ) {
226
217
?>
227
- <script type="text/javascript">gtag('event', 'begin_checkout', {
228
- 'items': <?php echo wp_json_encode ( $ gtag_items , JSON_PRETTY_PRINT ); ?> });</script>
218
+ <script type="text/javascript">if (gtag) {
219
+ gtag('event', 'begin_checkout', {
220
+ 'currency': '<?php echo esc_js ( $ currency ); ?> ',
221
+ 'items': <?php echo wp_json_encode ( $ gtag_items , JSON_PRETTY_PRINT ); ?> });
222
+ }</script>
229
223
<?php
230
224
}
231
225
}
@@ -235,17 +229,28 @@ public function track_programme_booking_view( $programme ) {
235
229
return ;
236
230
}
237
231
238
- $ gtag_items = [
239
- [
240
- 'item_id ' => 'PI_ ' . $ programme ["ProgrammeId " ],
241
- 'item_name ' => $ programme ["ProgrammeName " ],
242
- ]
232
+ $ currency = EDU ()->get_option ( 'eduadmin-currency ' , 'SEK ' );
233
+
234
+ $ gtag_items = [];
235
+
236
+ $ course_item = [
237
+ 'item_id ' => 'PI_ ' . $ programme ["ProgrammeId " ],
238
+ 'item_name ' => $ programme ["ProgrammeName " ],
243
239
];
244
240
241
+ if ( ! isset ( $ course_item ['price ' ] ) ) {
242
+ $ course_item = $ this ->getItemsPriced ( $ programme ['PriceNames ' ], $ course_item );
243
+ }
244
+
245
+ $ gtag_items [] = $ course_item ;
246
+
245
247
if ( count ( $ gtag_items ) > 0 ) {
246
248
?>
247
- <script type="text/javascript">gtag('event', 'begin_checkout', {
248
- 'items': <?php echo wp_json_encode ( $ gtag_items , JSON_PRETTY_PRINT ); ?> });</script>
249
+ <script type="text/javascript">if (gtag) {
250
+ gtag('event', 'begin_checkout', {
251
+ 'currency': '<?php echo esc_js ( $ currency ); ?> ',
252
+ 'items': <?php echo wp_json_encode ( $ gtag_items , JSON_PRETTY_PRINT ); ?> });
253
+ }</script>
249
254
<?php
250
255
}
251
256
}
@@ -257,46 +262,19 @@ public function track_booking_completed( $booking_info ) {
257
262
258
263
$ currency = EDU ()->get_option ( 'eduadmin-currency ' , 'SEK ' );
259
264
260
- $ event_info = null ;
261
265
$ transaction_id = null ;
262
- $ is_programme = false ;
263
266
264
267
if ( key_exists ( 'BookingId ' , $ booking_info ) ) {
265
- $ event_info = EDUAPI ()->OData ->Events ->GetItem ( $ booking_info ['EventId ' ] );
266
268
$ transaction_id = "B_ " . $ booking_info ['BookingId ' ];
267
269
} else if ( key_exists ( 'ProgrammeBookingId ' , $ booking_info ) ) {
268
- $ event_info = EDUAPI ()->OData ->ProgrammeStarts ->GetItem ( $ booking_info ['ProgrammeStartId ' ] );
269
270
$ transaction_id = "P_ " . $ booking_info ['ProgrammeBookingId ' ];
270
- $ is_programme = true ;
271
271
}
272
272
273
273
$ order_rows = [];
274
274
275
- if ( ! $ is_programme ) {
276
- $ row = [
277
- 'item_number ' => 'EV_ ' . $ event_info ['CourseTemplateId ' ],
278
- 'item_name ' => $ event_info ['EventName ' ],
279
- 'quantity ' => 1 ,
280
- 'price ' => 0 ,
281
- 'discount ' => 0 ,
282
- ];
283
-
284
- } else {
285
- $ row = [
286
- 'item_number ' => 'PSI_ ' . $ event_info ['ProgrammeStartId ' ],
287
- 'item_name ' => $ event_info ['ProgrammeStartName ' ],
288
- 'quantity ' => 1 ,
289
- 'price ' => 0 ,
290
- 'discount ' => 0 ,
291
- ];
292
-
293
- }
294
-
295
- $ order_rows [] = $ row ;
296
-
297
275
foreach ( $ booking_info ['OrderRows ' ] as $ order_row ) {
298
276
$ row = [
299
- 'item_number ' => 'OR_ ' . $ order_row ['OrderRowId ' ],
277
+ 'item_number ' => isset ( $ order_row [ ' ItemNumber ' ] ) ? $ order_row [ ' ItemNumber ' ] : 'OR_ ' . $ order_row ['OrderRowId ' ],
300
278
'item_name ' => $ order_row ['Description ' ],
301
279
'quantity ' => $ order_row ['Quantity ' ],
302
280
'price ' => $ order_row ['TotalPriceIncDiscount ' ] / $ order_row ['Quantity ' ],
@@ -308,13 +286,15 @@ public function track_booking_completed( $booking_info ) {
308
286
309
287
if ( count ( $ order_rows ) > 0 ) {
310
288
?>
311
- <script type="text/javascript">gtag('event', 'purchase', {
312
- 'transaction_id': '<?php echo esc_js ( $ transaction_id ); ?> ',
313
- 'currency': '<?php echo esc_js ( $ currency ); ?> ',
314
- 'value': <?php echo esc_js ( $ booking_info ["TotalPriceExVat " ] ); ?> ,
315
- 'tax': <?php echo esc_js ( $ booking_info ["VatSum " ] ); ?> ,
316
- 'items': <?php echo wp_json_encode ( $ order_rows , JSON_PRETTY_PRINT ); ?>
317
- });</script>
289
+ <script type="text/javascript">if (gtag) {
290
+ gtag('event', 'purchase', {
291
+ 'transaction_id': '<?php echo esc_js ( $ transaction_id ); ?> ',
292
+ 'currency': '<?php echo esc_js ( $ currency ); ?> ',
293
+ 'value': <?php echo esc_js ( $ booking_info ["TotalPriceExVat " ] ); ?> ,
294
+ 'tax': <?php echo esc_js ( $ booking_info ["VatSum " ] ); ?> ,
295
+ 'items': <?php echo wp_json_encode ( $ order_rows , JSON_PRETTY_PRINT ); ?>
296
+ });
297
+ }</script>
318
298
<?php
319
299
}
320
300
}
@@ -420,5 +400,23 @@ public function getItemsCategorized( $categories, array $course_item, array $gta
420
400
421
401
return $ gtag_items ;
422
402
}
403
+
404
+ public function getItemsPriced ( $ price_names , array $ course_item ): array {
405
+ $ lowest_price_name = null ;
406
+
407
+ if ( $ price_names == null ) {
408
+ return $ course_item ;
409
+ }
410
+
411
+ foreach ( $ price_names as $ price_name ) {
412
+ if ( $ lowest_price_name == null || $ price_name ['Price ' ] < $ lowest_price_name ['Price ' ] ) {
413
+ $ lowest_price_name = $ price_name ;
414
+ }
415
+ }
416
+
417
+ $ course_item ['price ' ] = $ lowest_price_name ['Price ' ];
418
+
419
+ return $ course_item ;
420
+ }
423
421
}
424
422
}
0 commit comments