Skip to content

Commit

Permalink
Updated the custom container loader
Browse files Browse the repository at this point in the history
  • Loading branch information
TorbenLundsgaard committed May 1, 2024
1 parent db2e53a commit 2ba5abe
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Find out about what's new in our [our release post](https://gtmkit.com/gtm-kit-1

#### Enhancements:
- Added advanced GCM settings: 'ads_data_redaction', 'url_passthrough' and 'wait_for_update'.
- Updated the custom container loader.

#### Bugfixes:

Expand Down
5 changes: 3 additions & 2 deletions src/Frontend/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,16 @@ public function get_gtm_script( string $gtm_id ): void {
$loader = $this->options->get( 'general', 'sgtm_container_identifier' ) ? $this->options->get( 'general', 'sgtm_container_identifier' ) : 'gtm';

if ( $this->options->get( 'general', 'sgtm_cookie_keeper' ) ) {
$gtm_id = preg_replace( '/^GTM\-/i', '', $gtm_id );
echo "/* Google Tag Manager with Stape.io Cookie Keeper */\n";
echo '!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,dl=n!="dataLayer"?"&l="+n:"";_.async=!0,_.src="https://' . esc_attr( $domain ) . '/"+f+".js?id=' . esc_attr( $gtm_id ) . '"+dl+T' . ( ( ! empty( Options::init()->get( 'general', 'gtm_auth' ) ) && ! empty( Options::init()->get( 'general', 'gtm_preview' ) ) ) ? "+'&gtm_auth=" . esc_attr( Options::init()->get( 'general', 'gtm_auth' ) ) . '&gtm_preview=' . esc_attr( Options::init()->get( 'general', 'gtm_preview' ) ) . "&gtm_cookies_win=x'" : '' ) . ',null===(d=m.parentNode)||void 0===d||d.insertBefore(_,m)}(window,document,"script","' . esc_js( $this->datalayer_name ) . '",0,0,"' . esc_attr( $loader ) . '","cookie")}();';
echo '!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="' . esc_js( $this->datalayer_name ) . '",o="' . esc_js( $gtm_id ) . '",a="https://' . esc_attr( $domain ) . '",i="",c="' . esc_attr( $loader ) . '",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' . ( ( ! empty( Options::init()->get( 'general', 'gtm_auth' ) ) && ! empty( Options::init()->get( 'general', 'gtm_preview' ) ) ) ? "+'&gtm_auth=" . esc_attr( Options::init()->get( 'general', 'gtm_auth' ) ) . '&gtm_preview=' . esc_attr( Options::init()->get( 'general', 'gtm_preview' ) ) . "&gtm_cookies_win=x'" : '' ) . ',null!=(f=E.parentNode)&&f.insertBefore(A,E)}();';
echo "\n/* End Google Tag Manager */\n";
} else {
echo "/* Google Tag Manager */\n";
echo "(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':\n";
echo "new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],\n";
echo "j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=\n";
echo "'https://" . esc_attr( $domain ) . '/' . esc_attr( $loader ) . ".js?id='+i+dl";
echo "'https://" . esc_attr( $domain ) . '/' . esc_attr( $loader ) . '.js?' . ( $loader === 'gtm' ) ? 'id' : 'st' . "='+i+dl";
echo ( ! empty( $this->options->get( 'general', 'gtm_auth' ) ) && ! empty( $this->options->get( 'general', 'gtm_preview' ) ) ) ? "+'&gtm_auth=" . esc_attr( $this->options->get( 'general', 'gtm_auth' ) ) . '&gtm_preview=' . esc_attr( $this->options->get( 'general', 'gtm_preview' ) ) . "&gtm_cookies_win=x'" : '';
echo ";f.parentNode.insertBefore(j,f);\n";
echo "})(window,document,'script','" . esc_js( $this->datalayer_name ) . "','" . esc_attr( $gtm_id ) . "');\n";
Expand Down

0 comments on commit 2ba5abe

Please sign in to comment.