Skip to content

Commit

Permalink
feat: Remove settings for GTM + GT and add extra info for items
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimple committed Jan 2, 2024
1 parent 3835854 commit bafb477
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 130 deletions.
250 changes: 124 additions & 126 deletions class/class-edu-google.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public function __construct() {
$this->init_form_fields();
$this->init_settings();

add_action( 'wp_head', array( $this, 'add_gtag_script' ) );

add_action( 'eduadmin-list-course-view', array( $this, 'track_list_course_view' ) );
add_action( 'eduadmin-list-event-view', array( $this, 'track_list_event_view' ) );
add_action( 'eduadmin-detail-view', array( $this, 'track_detail_view' ) );
Expand All @@ -36,82 +34,20 @@ public function __construct() {
*/
public function init_form_fields() {
$this->setting_fields = [
'enabled' => [
'enabled' => [
'title' => __( 'Enabled', 'eduadmin-analytics' ),
'type' => 'checkbox',
'description' => __( 'Enable Google Analytics / Tag Manager integration', 'eduadmin-analytics' ),
'default' => 'no',
],
'google-tag-manager' => [
'title' => __( 'Google Tag Manager ID', 'eduadmin-analytics' ),
'type' => 'text',
'description' => __( 'The ID of the Google Tag Manager', 'eduadmin-analytics' ),
'default' => '',
],
'google-tag' => [
'title' => __( 'Google Tag ID', 'eduadmin-analytics' ),
'type' => 'text',
'description' => __( 'The ID of the Google Tag', 'eduadmin-analytics' ),
'default' => '',
],
];
}

public function add_gtag_script() {
if ( 'no' === $this->get_option( 'enabled', 'no' ) ) {
return;
}

if ( ! empty( $this->get_option( 'google-tag-manager' ) ) ) {
?>
<!-- Google Tag Manager -->
<script>(function (w, d, s, l, i) {
w[l] = w[l] || [];
w[l].push({
'gtm.start':
new Date().getTime(), event: 'gtm.js'
});
var f = d.getElementsByTagName(s)[0],
j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : '';
j.async = true;
j.src =
'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'eduAdminDataLayer', '<?php echo esc_js( $this->get_option( 'google-tag-manager' ) ); ?>');</script>
<!-- End Google Tag Manager -->
<?php
}

if ( ! empty( $this->get_option( 'google-tag' ) ) ) {
$currency = EDU()->get_option( 'eduadmin-currency', 'SEK' );
?>
<script async
src="https://www.googletagmanager.com/gtag/js?id=<?php echo esc_attr( $this->get_option( 'google-tag' ) ); ?>&l=eduAdminDataLayer"></script>
<script>
window.eduAdminDataLayer = window.eduAdminDataLayer || [];

function gtag() {
eduAdminDataLayer.push(arguments);
}

gtag('js', new Date());
gtag('config', '<?php echo esc_js( $this->get_option( 'google-tag' ) ); ?>', {
'currency': '<?php echo esc_js( $currency ); ?>',
});
</script>
<?php
}
}

public function is_configured_and_enabled() {
if ( 'no' === $this->get_option( 'enabled', 'no' ) ) {
return false;
}

if ( empty( $this->get_option( 'google-tag' ) ) && empty( $this->get_option( 'google-tag-manager' ) ) ) {
return false;
}

return true;
}

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

if ( isset( $course['Events'] ) ) {
foreach ( $course['Events'] as $event ) {
$course_item = $this->getItemsPriced( $event['PriceNames'], $course_item );
}
}

if ( ! isset( $course_item['price'] ) ) {
$course_item = $this->getItemsPriced( $course['PriceNames'], $course_item );
}
$gtag_items = $this->getItemsCategorized( $course["Categories"], $course_item, $gtag_items );
}

if ( count( $gtag_items ) > 0 ) {
?>
<script type="text/javascript">gtag('event', 'view_item_list', {
'item_list_id': 'course_list',
'item_list_name': 'Course list',
'items': <?php echo wp_json_encode( $gtag_items, JSON_PRETTY_PRINT ); ?> });</script>
<script type="text/javascript">if (gtag) {
gtag('event', 'view_item_list', {
'item_list_id': 'course_list',
'item_list_name': 'Course list',
'items': <?php echo wp_json_encode( $gtag_items, JSON_PRETTY_PRINT ); ?> });
}
</script>
<?php
}
}
Expand All @@ -147,21 +95,30 @@ public function track_list_event_view( $events ) {
}

$gtag_items = [];

foreach ( $events as $course ) {
$course_item = [
'item_id' => 'EV_' . $course["CourseTemplateId"],
'item_name' => $course["CourseName"],
];

$course_item = $this->getItemsPriced( $course['PriceNames'], $course_item );

if ( ! isset( $course_item['price'] ) ) {
$course_item = $this->getItemsPriced( $course['CourseTemplate']['PriceNames'], $course_item );
}

$gtag_items = $this->getItemsCategorized( $course["Categories"], $course_item, $gtag_items );
}

if ( count( $gtag_items ) > 0 ) {
?>
<script type="text/javascript">gtag('event', 'view_item_list', {
'item_list_id': 'event_list',
'item_list_name': 'Event list',
'items': <?php echo wp_json_encode( $gtag_items, JSON_PRETTY_PRINT ); ?> });</script>
<script type="text/javascript">if (gtag) {
gtag('event', 'view_item_list', {
'item_list_id': 'event_list',
'item_list_name': 'Event list',
'items': <?php echo wp_json_encode( $gtag_items, JSON_PRETTY_PRINT ); ?> });
}</script>
<?php
}
}
Expand All @@ -171,19 +128,32 @@ public function track_detail_view( $course_template ) {
return;
}

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

$gtag_items = [];

$course_item = [
'item_id' => 'CTI_' . $course_template["CourseTemplateId"],
'item_name' => $course_template["CourseName"],
];

foreach ( $course_template['Events'] as $event ) {
$course_item = $this->getItemsPriced( $event['PriceNames'], $course_item );
}

if ( ! isset( $course_item['price'] ) ) {
$course_item = $this->getItemsPriced( $course_template['PriceNames'], $course_item );
}

$gtag_items = $this->getItemsCategorized( $course_template["Categories"], $course_item, $gtag_items );

if ( count( $gtag_items ) > 0 ) {
?>
<script type="text/javascript">gtag('event', 'view_item', {
'items': <?php echo wp_json_encode( $gtag_items, JSON_PRETTY_PRINT ); ?> });</script>
<script type="text/javascript">if (gtag) {
gtag('event', 'view_item', {
'currency': '<?php echo esc_js( $currency ); ?>',
'items': <?php echo wp_json_encode( $gtag_items, JSON_PRETTY_PRINT ); ?> });
}</script>
<?php
}
}
Expand All @@ -193,17 +163,28 @@ public function track_programme_detail_view( $programme ) {
return;
}

$gtag_items = [
[
'item_id' => 'PI_' . $programme["ProgrammeId"],
'item_name' => $programme["ProgrammeName"],
]
$currency = EDU()->get_option( 'eduadmin-currency', 'SEK' );

$gtag_items = [];

$course_item = [
'item_id' => 'PI_' . $programme["ProgrammeId"],
'item_name' => $programme["ProgrammeName"],
];

if ( ! isset( $course_item['price'] ) ) {
$course_item = $this->getItemsPriced( $programme['PriceNames'], $course_item );
}

$gtag_items[] = $course_item;

if ( count( $gtag_items ) > 0 ) {
?>
<script type="text/javascript">gtag('event', 'view_item', {
'items': <?php echo wp_json_encode( $gtag_items, JSON_PRETTY_PRINT ); ?> });</script>
<script type="text/javascript">if (gtag) {
gtag('event', 'view_item', {
'currency': '<?php echo esc_js( $currency ); ?>',
'items': <?php echo wp_json_encode( $gtag_items, JSON_PRETTY_PRINT ); ?> });
}</script>
<?php
}
}
Expand All @@ -213,19 +194,32 @@ public function track_booking_view( $course_template ) {
return;
}

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

$gtag_items = [];

$course_item = [
'item_id' => 'CTI_' . $course_template["CourseTemplateId"],
'item_name' => $course_template["CourseName"],
];

foreach ( $course_template['Events'] as $event ) {
$course_item = $this->getItemsPriced( $event['PriceNames'], $course_item );
}

if ( ! isset( $course_item['price'] ) ) {
$course_item = $this->getItemsPriced( $course_template['PriceNames'], $course_item );
}

$gtag_items = $this->getItemsCategorized( $course_template["Categories"], $course_item, $gtag_items );

if ( count( $gtag_items ) > 0 ) {
?>
<script type="text/javascript">gtag('event', 'begin_checkout', {
'items': <?php echo wp_json_encode( $gtag_items, JSON_PRETTY_PRINT ); ?> });</script>
<script type="text/javascript">if (gtag) {
gtag('event', 'begin_checkout', {
'currency': '<?php echo esc_js( $currency ); ?>',
'items': <?php echo wp_json_encode( $gtag_items, JSON_PRETTY_PRINT ); ?> });
}</script>
<?php
}
}
Expand All @@ -235,17 +229,28 @@ public function track_programme_booking_view( $programme ) {
return;
}

$gtag_items = [
[
'item_id' => 'PI_' . $programme["ProgrammeId"],
'item_name' => $programme["ProgrammeName"],
]
$currency = EDU()->get_option( 'eduadmin-currency', 'SEK' );

$gtag_items = [];

$course_item = [
'item_id' => 'PI_' . $programme["ProgrammeId"],
'item_name' => $programme["ProgrammeName"],
];

if ( ! isset( $course_item['price'] ) ) {
$course_item = $this->getItemsPriced( $programme['PriceNames'], $course_item );
}

$gtag_items[] = $course_item;

if ( count( $gtag_items ) > 0 ) {
?>
<script type="text/javascript">gtag('event', 'begin_checkout', {
'items': <?php echo wp_json_encode( $gtag_items, JSON_PRETTY_PRINT ); ?> });</script>
<script type="text/javascript">if (gtag) {
gtag('event', 'begin_checkout', {
'currency': '<?php echo esc_js( $currency ); ?>',
'items': <?php echo wp_json_encode( $gtag_items, JSON_PRETTY_PRINT ); ?> });
}</script>
<?php
}
}
Expand All @@ -257,46 +262,19 @@ public function track_booking_completed( $booking_info ) {

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

$event_info = null;
$transaction_id = null;
$is_programme = false;

if ( key_exists( 'BookingId', $booking_info ) ) {
$event_info = EDUAPI()->OData->Events->GetItem( $booking_info['EventId'] );
$transaction_id = "B_" . $booking_info['BookingId'];
} else if ( key_exists( 'ProgrammeBookingId', $booking_info ) ) {
$event_info = EDUAPI()->OData->ProgrammeStarts->GetItem( $booking_info['ProgrammeStartId'] );
$transaction_id = "P_" . $booking_info['ProgrammeBookingId'];
$is_programme = true;
}

$order_rows = [];

if ( ! $is_programme ) {
$row = [
'item_number' => 'EV_' . $event_info['CourseTemplateId'],
'item_name' => $event_info['EventName'],
'quantity' => 1,
'price' => 0,
'discount' => 0,
];

} else {
$row = [
'item_number' => 'PSI_' . $event_info['ProgrammeStartId'],
'item_name' => $event_info['ProgrammeStartName'],
'quantity' => 1,
'price' => 0,
'discount' => 0,
];

}

$order_rows[] = $row;

foreach ( $booking_info['OrderRows'] as $order_row ) {
$row = [
'item_number' => 'OR_' . $order_row['OrderRowId'],
'item_number' => isset( $order_row['ItemNumber'] ) ? $order_row['ItemNumber'] : 'OR_' . $order_row['OrderRowId'],
'item_name' => $order_row['Description'],
'quantity' => $order_row['Quantity'],
'price' => $order_row['TotalPriceIncDiscount'] / $order_row['Quantity'],
Expand All @@ -308,13 +286,15 @@ public function track_booking_completed( $booking_info ) {

if ( count( $order_rows ) > 0 ) {
?>
<script type="text/javascript">gtag('event', 'purchase', {
'transaction_id': '<?php echo esc_js( $transaction_id ); ?>',
'currency': '<?php echo esc_js( $currency ); ?>',
'value': <?php echo esc_js( $booking_info["TotalPriceExVat"] ); ?>,
'tax': <?php echo esc_js( $booking_info["VatSum"] ); ?>,
'items': <?php echo wp_json_encode( $order_rows, JSON_PRETTY_PRINT ); ?>
});</script>
<script type="text/javascript">if (gtag) {
gtag('event', 'purchase', {
'transaction_id': '<?php echo esc_js( $transaction_id ); ?>',
'currency': '<?php echo esc_js( $currency ); ?>',
'value': <?php echo esc_js( $booking_info["TotalPriceExVat"] ); ?>,
'tax': <?php echo esc_js( $booking_info["VatSum"] ); ?>,
'items': <?php echo wp_json_encode( $order_rows, JSON_PRETTY_PRINT ); ?>
});
}</script>
<?php
}
}
Expand Down Expand Up @@ -420,5 +400,23 @@ public function getItemsCategorized( $categories, array $course_item, array $gta

return $gtag_items;
}

public function getItemsPriced( $price_names, array $course_item ): array {
$lowest_price_name = null;

if ( $price_names == null ) {
return $course_item;
}

foreach ( $price_names as $price_name ) {
if ( $lowest_price_name == null || $price_name['Price'] < $lowest_price_name['Price'] ) {
$lowest_price_name = $price_name;
}
}

$course_item['price'] = $lowest_price_name['Price'];

return $course_item;
}
}
}
Loading

0 comments on commit bafb477

Please sign in to comment.