Skip to content

Instantly share code, notes, and snippets.

@jlapitan
Last active June 29, 2016 06:52
Show Gist options
  • Save jlapitan/88f19cf4c5e9047c711cc2cd0cb894fe to your computer and use it in GitHub Desktop.
Save jlapitan/88f19cf4c5e9047c711cc2cd0cb894fe to your computer and use it in GitHub Desktop.
<?php
function gtm4wp_get_the_gtm_tag() {
global $gtm4wp_options, $gtm4wp_datalayer_name, $gtm4wp_container_code_written;
$_gtm_tag = '';
if ( ( $gtm4wp_options[ GTM4WP_OPTION_GTM_CODE ] != "" ) && ( ! $gtm4wp_container_code_written ) ) {
$_gtm_codes = explode( ",", str_replace( array(";"," "), array(",",""), $gtm4wp_options[ GTM4WP_OPTION_GTM_CODE ] ) );
foreach( $_gtm_codes as $one_gtm_code ) {
$_gtm_tag .= '
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=' . $one_gtm_code . '"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<script data-cfasync="false">(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=
\'//www.googletagmanager.com/gtm.\''.'+\'js?id=\'+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,\'script\',\'' . $gtm4wp_datalayer_name . '\',\'' . $one_gtm_code . '\');</script>';
}
$_gtm_tag .= '
<!-- End Google Tag Manager -->';
$_gtm_tag = apply_filters( GTM4WP_WPFILTER_GETTHEGTMTAG, $_gtm_tag );
$gtm4wp_container_code_written = true;
}
return $_gtm_tag;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment