Skip to content

Commit

Permalink
Fix GTM Code
Browse files Browse the repository at this point in the history
  • Loading branch information
plance committed Sep 18, 2024
1 parent 41ffb00 commit 4f56cec
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
27 changes: 7 additions & 20 deletions includes/class-gtm-server-side-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static function get_data_layer_custom_event_name() {
*
* @return bool
*/
private static function has_gtm_container_identifier() {
public static function has_gtm_container_identifier() {
return ! empty( self::get_raw_gtm_container_identifier() );
}

Expand All @@ -136,12 +136,12 @@ public static function get_gtm_container_id() {
GTM_SERVER_SIDE_FIELD_WEB_CONTAINER_ID,
function() use ( $container_id ) {
$query_ends = array(
'&page=1',
'&page=2',
'&page=3',
'&apiKey=' . mb_substr( md5( self::get_raw_gtm_container_identifier() ), 0, 8 ),
'&sort=asc',
'&sort=desc',
'page=1',
'page=2',
'page=3',
'apiKey=' . mb_substr( md5( self::get_raw_gtm_container_identifier() ), 0, 8 ),
'sort=asc',
'sort=desc',
);
$random_end = array_rand( $query_ends );
$query_end = $query_ends[ $random_end ];
Expand Down Expand Up @@ -207,19 +207,6 @@ function() {
return $identifier;
}

/**
* Return GTM param id.
*
* @return string
*/
public static function get_gtm_param_id() {
if ( self::has_gtm_container_identifier() ) {
return 'st';
}

return 'id';
}

/**
* Enable or disable data layer ecommerce.
*
Expand Down
31 changes: 28 additions & 3 deletions includes/class-gtm-server-side-tracking-code.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,18 @@ public function init() {
*/
public function head() {
if ( GTM_Server_Side_Helpers::is_enable_cookie_keeper() ) {

$this->print_cookie_keeper_gtm_code();
} else {
$this->print_default_gtm_code();
return;
}

if ( GTM_Server_Side_Helpers::has_gtm_container_identifier() ) {

$this->print_stape_gtm_code();
return;
}

$this->print_default_gtm_code();
}

/**
Expand Down Expand Up @@ -87,7 +95,24 @@ public function body() {
private function print_default_gtm_code() {
echo "
<!-- 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);j.async=true;j.src=\"" . esc_js( GTM_Server_Side_Helpers::get_gtm_container_url() ) . '/' . esc_js( GTM_Server_Side_Helpers::get_gtm_container_identifier() ) . '.js?' . esc_js( GTM_Server_Side_Helpers::get_gtm_param_id() ) . "\"+i;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer','" . esc_js( GTM_Server_Side_Helpers::get_gtm_container_id() ) . "');</script>
<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=
'" . esc_js( GTM_Server_Side_Helpers::get_gtm_container_url() ) . '/' . esc_js( GTM_Server_Side_Helpers::get_gtm_container_identifier() ) . ".js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','" . esc_js( GTM_Server_Side_Helpers::get_gtm_container_id() ) . "');</script>
<!-- End Google Tag Manager -->
";
}

/**
* Print stape GTM Code.
*
* @return void
*/
private function print_stape_gtm_code() {
echo "
<!-- 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);j.async=true;j.src=\"" . esc_js( GTM_Server_Side_Helpers::get_gtm_container_url() ) . '/' . esc_js( GTM_Server_Side_Helpers::get_gtm_container_identifier() ) . ".js?\"+i;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer','" . esc_js( GTM_Server_Side_Helpers::get_gtm_container_id() ) . "');</script>
<!-- End Google Tag Manager -->
";
}
Expand Down

0 comments on commit 4f56cec

Please sign in to comment.