Skip to content

Commit

Permalink
Custom loader update
Browse files Browse the repository at this point in the history
  • Loading branch information
plance committed Mar 6, 2024
1 parent ba2cd61 commit 0aa1211
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
5 changes: 4 additions & 1 deletion README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: gtmserver,bukashk0zzz
Tags: google tag manager, google tag manager server side, gtm, gtm server side, tag manager, tagmanager, analytics, google, serverside, server-side, gtag
Requires at least: 5.2.0
Tested up to: 6.4.2
Stable tag: 2.1.12
Stable tag: 2.1.13
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -66,6 +66,9 @@ Yes. <a href="https://stape.io/blog/how-to-set-up-facebook-conversion-api">How t
4. Menu item in the settings panel.

== Changelog ==
= 2.1.13 =
* Custom loader update

= 2.1.12 =
* Add more cookies to the list of cookies that are sent to the server

Expand Down
2 changes: 1 addition & 1 deletion gtm-server-side.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Plugin Name: GTM Server Side
* Plugin URI: https://wordpress.org/plugins/gtm-server-side/
* Description: Enhance conversion tracking by implementing server-side tagging using server Google Tag Manager container. Effortlessly configure data layer events in web GTM, send webhooks, set up custom loader, and extend cookie lifetime.
* Version: 2.1.12
* Version: 2.1.13
* Author: Stape
* Author URI: https://stape.io
* License: GPL-2.0+
Expand Down
37 changes: 37 additions & 0 deletions includes/class-gtm-server-side-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ public static function get_gtm_container_id() {
return self::get_option( GTM_SERVER_SIDE_FIELD_WEB_CONTAINER_ID );
}

/**
* Return filtering GTM web container ID.
*
* @return string
*/
public static function get_gtm_filtering_container_id() {
$container_id = self::get_gtm_container_id();

if ( self::has_gtm_container_identifier() ) {
$container_id = preg_replace( '/^GTM\-/i', '', $container_id );
}

return $container_id;
}

/**
* Return GTM web container url.
*
Expand All @@ -85,6 +100,15 @@ public static function get_gtm_container_url() {
return $url;
}

/**
* Check has gtm container identifier or not.
*
* @return bool
*/
public static function has_gtm_container_identifier() {
return ! empty( self::get_option( GTM_SERVER_SIDE_FIELD_WEB_IDENTIFIER ) );
}

/**
* Return GTM identifier.
*
Expand All @@ -100,6 +124,19 @@ public static function get_gtm_container_identifier() {
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
10 changes: 5 additions & 5 deletions includes/class-gtm-server-side-tracking-code.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function body() {

echo '
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="' . esc_attr( GTM_Server_Side_Helpers::get_gtm_container_url() ) . '/ns.html?id=' . esc_attr( GTM_Server_Side_Helpers::get_gtm_container_id() ) . '"
<noscript><iframe src="' . esc_attr( GTM_Server_Side_Helpers::get_gtm_container_url() ) . '/ns.html?id=' . esc_attr( GTM_Server_Side_Helpers::get_gtm_filtering_container_id() ) . '"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
';
Expand All @@ -90,8 +90,8 @@ private function print_default_gtm_code() {
<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_attr( GTM_Server_Side_Helpers::get_gtm_container_url() ) . '/' . esc_attr( 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>
'" . esc_attr( GTM_Server_Side_Helpers::get_gtm_container_url() ) . '/' . esc_attr( GTM_Server_Side_Helpers::get_gtm_container_identifier() ) . '.js?' . esc_attr( GTM_Server_Side_Helpers::get_gtm_param_id() ) . "='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','" . esc_js( GTM_Server_Side_Helpers::get_gtm_filtering_container_id() ) . "');</script>
<!-- End Google Tag Manager -->
";
}
Expand All @@ -104,8 +104,8 @@ private function print_default_gtm_code() {
private function print_cookie_keeper_gtm_code() {
echo '
<!-- GTM Container Loader By GTM Server Side plugin -->
<script>!function(){"use strict";function e(e,t,o){return void 0===t&&(t=""),"cookie"===e?function(e){for(var t=0,o=document.cookie.split(";");t<o.length;t++){var r=o[t].split("=");if(r[0].trim()===e)return r[1]}}(t):"localStorage"===e?(r=t,localStorage.getItem(r)):"jsVariable"===e?window[t]:"cssSelector"===e?(n=t,i=o,a=document.querySelector(n),i?null==a?void 0:a.getAttribute(i):null==a?void 0:a.textContent):void console.warn("invalid uid source",e);var r,n,i,a}!function(t,o,r,n,i,a,c,l,s,u){var d,v,E,I;try{v=l&&(E=navigator.userAgent,(I=/Version\/([0-9\._]+)(.*Mobile)?.*Safari.*/.exec(E))&&parseFloat(I[1])>=16.4)?e(l,"_sbp",""):void 0}catch(e){console.error(e)}var g=t;g[n]=g[n]||[],g[n].push({"gtm.start":(new Date).getTime(),event:"gtm.js"});var m=o.getElementsByTagName(r)[0],T=v?"&bi="+encodeURIComponent(v):"",_=o.createElement(r),f=v?"kp"+c:c;_.async=!0,_.src="' . esc_attr( GTM_Server_Side_Helpers::get_gtm_container_url() ) . '/"+f+".js?id=' . esc_js( GTM_Server_Side_Helpers::get_gtm_container_id() ) . '"+T,null===(d=m.parentNode)||void 0===d||d.insertBefore(_,m)}(window,document,"script","dataLayer",0,0,"' . esc_attr( GTM_Server_Side_Helpers::get_gtm_container_identifier() ) . '","cookie")}();</script>
<!-- END of GTM Container Loader By GTM Server Side plugin -->
<script>!function(){"use strict";function l(e){for(var t=e,r=0,n=document.cookie.split(";");r<n.length;r++){var o=n[r].split("=");if(o[0].trim()===t)return o[1]}}function s(e){return localStorage.getItem(e)}function u(e){return window[e]}function d(e,t){e=document.querySelector(e);return t?null==e?void 0:e.getAttribute(t):null==e?void 0:e.textContent}var e=window,t=document,r="script",n="dataLayer",o="' . esc_js( GTM_Server_Side_Helpers::get_gtm_filtering_container_id() ) . '",a="' . esc_attr( GTM_Server_Side_Helpers::get_gtm_container_url() ) . '",i="",c="' . esc_attr( GTM_Server_Side_Helpers::get_gtm_container_identifier() ) . '",E="cookie",I="_sbp",v="",g=!1;try{var g=!!E&&(m=navigator.userAgent,!!(m=new RegExp("Version/([0-9._]+)(.*Mobile)?.*Safari.*").exec(m)))&&16.4<=parseFloat(m[1]),A="stapeUserId"===E,f=g&&!A?function(e,t,r){void 0===t&&(t="");var n={cookie:l,localStorage:s,jsVariable:u,cssSelector:d},t=Array.isArray(t)?t:[t];if(e&&n[e])for(var o=n[e],a=0,i=t;a<i.length;a++){var c=i[a],c=r?o(c,r):o(c);if(c)return c}else console.warn("invalid uid source",e)}(E,I,v):void 0;g=g&&(!!f||A)}catch(e){console.error(e)}var m=e,E=(m[n]=m[n]||[],m[n].push({"gtm.start":(new Date).getTime(),event:"gtm.js"}),t.getElementsByTagName(r)[0]),I="dataLayer"===n?"":"&l="+n,v=f?"&bi="+encodeURIComponent(f):"",A=t.createElement(r),e=g?"kp"+c:c,n=!g&&i?i:a;A.async=!0,A.src=n+"/"+e+".js?st="+o+I+v,null!=(f=E.parentNode)&&f.insertBefore(A,E)}();</script>
<!-- END of GTM Container Loader By GTM Server Side plugin -->
';
}

Expand Down
8 changes: 4 additions & 4 deletions includes/class-gtm-server-side-webhook-purchase.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ public function woocommerce_new_order( $order_id, $order ) {
'FPGCLAW' => filter_input( INPUT_COOKIE, 'FPGCLAW', FILTER_DEFAULT ),
'_gcl_aw' => filter_input( INPUT_COOKIE, '_gcl_aw', FILTER_DEFAULT ),
'ttclid' => filter_input( INPUT_COOKIE, 'ttclid', FILTER_DEFAULT ),
'_dcid' => filter_input( INPUT_COOKIE, '_dcid', FILTER_DEFAULT ),
'FPID' => filter_input( INPUT_COOKIE, 'FPID', FILTER_DEFAULT ),
'FPLC' => filter_input( INPUT_COOKIE, 'FPLC', FILTER_DEFAULT ),
'_dcid' => filter_input( INPUT_COOKIE, '_dcid', FILTER_DEFAULT ),
'FPID' => filter_input( INPUT_COOKIE, 'FPID', FILTER_DEFAULT ),
'FPLC' => filter_input( INPUT_COOKIE, 'FPLC', FILTER_DEFAULT ),
);
$request_cookies = array_filter( $request_cookies );

if ( ! empty( $request_cookies ) ) {
$request['cookies'] = $request_cookies;

if (isset($request_cookies['_dcid'])) {
if ( isset( $request_cookies['_dcid'] ) ) {
$request['client_id'] = $request_cookies['_dcid'];
}
}
Expand Down

0 comments on commit 0aa1211

Please sign in to comment.