Skip to content

Commit bafb477

Browse files
committed
feat: Remove settings for GTM + GT and add extra info for items
1 parent 3835854 commit bafb477

File tree

5 files changed

+128
-130
lines changed

5 files changed

+128
-130
lines changed

class/class-edu-google.php

+124-126
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ public function __construct() {
1717
$this->init_form_fields();
1818
$this->init_settings();
1919

20-
add_action( 'wp_head', array( $this, 'add_gtag_script' ) );
21-
2220
add_action( 'eduadmin-list-course-view', array( $this, 'track_list_course_view' ) );
2321
add_action( 'eduadmin-list-event-view', array( $this, 'track_list_event_view' ) );
2422
add_action( 'eduadmin-detail-view', array( $this, 'track_detail_view' ) );
@@ -36,82 +34,20 @@ public function __construct() {
3634
*/
3735
public function init_form_fields() {
3836
$this->setting_fields = [
39-
'enabled' => [
37+
'enabled' => [
4038
'title' => __( 'Enabled', 'eduadmin-analytics' ),
4139
'type' => 'checkbox',
4240
'description' => __( 'Enable Google Analytics / Tag Manager integration', 'eduadmin-analytics' ),
4341
'default' => 'no',
4442
],
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-
],
5743
];
5844
}
5945

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-
10646
public function is_configured_and_enabled() {
10747
if ( 'no' === $this->get_option( 'enabled', 'no' ) ) {
10848
return false;
10949
}
11050

111-
if ( empty( $this->get_option( 'google-tag' ) ) && empty( $this->get_option( 'google-tag-manager' ) ) ) {
112-
return false;
113-
}
114-
11551
return true;
11652
}
11753

@@ -128,15 +64,27 @@ public function track_list_course_view( $courses ) {
12864
'item_name' => $course["CourseName"],
12965
];
13066

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+
}
13176
$gtag_items = $this->getItemsCategorized( $course["Categories"], $course_item, $gtag_items );
13277
}
13378

13479
if ( count( $gtag_items ) > 0 ) {
13580
?>
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>
14088
<?php
14189
}
14290
}
@@ -147,21 +95,30 @@ public function track_list_event_view( $events ) {
14795
}
14896

14997
$gtag_items = [];
98+
15099
foreach ( $events as $course ) {
151100
$course_item = [
152101
'item_id' => 'EV_' . $course["CourseTemplateId"],
153102
'item_name' => $course["CourseName"],
154103
];
155104

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+
156111
$gtag_items = $this->getItemsCategorized( $course["Categories"], $course_item, $gtag_items );
157112
}
158113

159114
if ( count( $gtag_items ) > 0 ) {
160115
?>
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>
165122
<?php
166123
}
167124
}
@@ -171,19 +128,32 @@ public function track_detail_view( $course_template ) {
171128
return;
172129
}
173130

131+
$currency = EDU()->get_option( 'eduadmin-currency', 'SEK' );
132+
174133
$gtag_items = [];
175134

176135
$course_item = [
177136
'item_id' => 'CTI_' . $course_template["CourseTemplateId"],
178137
'item_name' => $course_template["CourseName"],
179138
];
180139

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+
181148
$gtag_items = $this->getItemsCategorized( $course_template["Categories"], $course_item, $gtag_items );
182149

183150
if ( count( $gtag_items ) > 0 ) {
184151
?>
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>
187157
<?php
188158
}
189159
}
@@ -193,17 +163,28 @@ public function track_programme_detail_view( $programme ) {
193163
return;
194164
}
195165

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"],
201173
];
202174

175+
if ( ! isset( $course_item['price'] ) ) {
176+
$course_item = $this->getItemsPriced( $programme['PriceNames'], $course_item );
177+
}
178+
179+
$gtag_items[] = $course_item;
180+
203181
if ( count( $gtag_items ) > 0 ) {
204182
?>
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>
207188
<?php
208189
}
209190
}
@@ -213,19 +194,32 @@ public function track_booking_view( $course_template ) {
213194
return;
214195
}
215196

197+
$currency = EDU()->get_option( 'eduadmin-currency', 'SEK' );
198+
216199
$gtag_items = [];
217200

218201
$course_item = [
219202
'item_id' => 'CTI_' . $course_template["CourseTemplateId"],
220203
'item_name' => $course_template["CourseName"],
221204
];
222205

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+
223214
$gtag_items = $this->getItemsCategorized( $course_template["Categories"], $course_item, $gtag_items );
224215

225216
if ( count( $gtag_items ) > 0 ) {
226217
?>
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>
229223
<?php
230224
}
231225
}
@@ -235,17 +229,28 @@ public function track_programme_booking_view( $programme ) {
235229
return;
236230
}
237231

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"],
243239
];
244240

241+
if ( ! isset( $course_item['price'] ) ) {
242+
$course_item = $this->getItemsPriced( $programme['PriceNames'], $course_item );
243+
}
244+
245+
$gtag_items[] = $course_item;
246+
245247
if ( count( $gtag_items ) > 0 ) {
246248
?>
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>
249254
<?php
250255
}
251256
}
@@ -257,46 +262,19 @@ public function track_booking_completed( $booking_info ) {
257262

258263
$currency = EDU()->get_option( 'eduadmin-currency', 'SEK' );
259264

260-
$event_info = null;
261265
$transaction_id = null;
262-
$is_programme = false;
263266

264267
if ( key_exists( 'BookingId', $booking_info ) ) {
265-
$event_info = EDUAPI()->OData->Events->GetItem( $booking_info['EventId'] );
266268
$transaction_id = "B_" . $booking_info['BookingId'];
267269
} else if ( key_exists( 'ProgrammeBookingId', $booking_info ) ) {
268-
$event_info = EDUAPI()->OData->ProgrammeStarts->GetItem( $booking_info['ProgrammeStartId'] );
269270
$transaction_id = "P_" . $booking_info['ProgrammeBookingId'];
270-
$is_programme = true;
271271
}
272272

273273
$order_rows = [];
274274

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-
297275
foreach ( $booking_info['OrderRows'] as $order_row ) {
298276
$row = [
299-
'item_number' => 'OR_' . $order_row['OrderRowId'],
277+
'item_number' => isset( $order_row['ItemNumber'] ) ? $order_row['ItemNumber'] : 'OR_' . $order_row['OrderRowId'],
300278
'item_name' => $order_row['Description'],
301279
'quantity' => $order_row['Quantity'],
302280
'price' => $order_row['TotalPriceIncDiscount'] / $order_row['Quantity'],
@@ -308,13 +286,15 @@ public function track_booking_completed( $booking_info ) {
308286

309287
if ( count( $order_rows ) > 0 ) {
310288
?>
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>
318298
<?php
319299
}
320300
}
@@ -420,5 +400,23 @@ public function getItemsCategorized( $categories, array $course_item, array $gta
420400

421401
return $gtag_items;
422402
}
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+
}
423421
}
424422
}

0 commit comments

Comments
 (0)