$value ) {
if ( substr( $name, 0, 5 ) === 'HTTP_' ) {
$headers[ str_replace( ' ', '-', ucwords( strtolower( str_replace( '_', ' ', substr( $name, 5 ) ) ) ) ) ] = $value;
}
}
return $headers;
}
}
/**
* Generates an opening ';
}
if ( ( '' !== $gtm4wp_options[ GTM4WP_OPTION_GTM_CODE ] ) && ( ! $gtm4wp_container_code_written ) ) {
$_gtm_codes = explode( ',', str_replace( array( ';', ' ' ), array( ',', '' ), $gtm4wp_options[ GTM4WP_OPTION_GTM_CODE ] ) );
if ( ( '' !== $gtm4wp_options[ GTM4WP_OPTION_ENV_GTM_AUTH ] ) && ( '' !== $gtm4wp_options[ GTM4WP_OPTION_ENV_GTM_PREVIEW ] ) ) {
$_gtm_env = '>m_auth=' . esc_attr( $gtm4wp_options[ GTM4WP_OPTION_ENV_GTM_AUTH ] ) . '>m_preview=' . esc_attr( $gtm4wp_options[ GTM4WP_OPTION_ENV_GTM_PREVIEW ] ) . '>m_cookies_win=x';
} else {
$_gtm_env = '';
}
// for PHP 7- compatibility.
if ( ! defined( 'FILTER_FLAG_HOSTNAME' ) ) {
define( 'FILTER_FLAG_HOSTNAME', 0 );
}
$_gtm_domain_test = ( '' === $gtm4wp_options[ GTM4WP_OPTION_GTMDOMAIN ] ) ? 'www.googletagmanager.com' : strtolower( $gtm4wp_options[ GTM4WP_OPTION_GTMDOMAIN ] );
$_gtm_domain_name = filter_var( $_gtm_domain_test, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME );
if ( false === $_gtm_domain_name ) {
$_gtm_domain_name = 'www.googletagmanager.com';
}
foreach ( $_gtm_codes as $one_gtm_id ) {
if ( preg_match( '/^GTM-[A-Z0-9]+$/', $one_gtm_id ) ) {
$_gtm_tag .= '
';
}
}
$_gtm_tag .= '
';
$gtm4wp_container_code_written = true;
}
return $_gtm_tag;
}
add_filter(
'safe_style_css',
function( $styles ) {
$styles[] = 'display';
$styles[] = 'visibility';
return $styles;
}
);
/**
* Outputs a HTML code that includes the noscript/iframe part of the Google Tag Manager container.
* Can be used to manually place the snippet next to the opening body tag if the installed template
* does not support the wp_body_open hook.
*
* @see https://developer.wordpress.org/reference/functions/wp_body_open/
*
* @return void
*/
function gtm4wp_the_gtm_tag() {
echo wp_kses(
gtm4wp_get_the_gtm_tag(),
array_merge(
gtm4wp_get_sanitize_script_block_rules(),
array(
'noscript' => array(),
'iframe' => array(
'src' => array(),
'height' => array(),
'width' => array(),
'style' => array(),
'aria-hidden' => array(),
),
)
)
);
}
/**
* Function executed during wp_enqueue_scripts.
* Loads JavaScript files based on plugin options that are turned on to prevent bloating the frontend.
*
* @see https://developer.wordpress.org/reference/hooks/wp_enqueue_scripts/
*
* @return void
*/
function gtm4wp_enqueue_scripts() {
global $gtm4wp_options, $gtp4wp_script_path;
if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WPCF7 ] ) {
$in_footer = (bool) apply_filters( 'gtm4wp_' . GTM4WP_OPTION_INTEGRATE_WPCF7, true );
wp_enqueue_script( 'gtm4wp-contact-form-7-tracker', $gtp4wp_script_path . 'gtm4wp-contact-form-7-tracker.js', array(), GTM4WP_VERSION, $in_footer );
}
if ( $gtm4wp_options[ GTM4WP_OPTION_EVENTS_FORMMOVE ] ) {
$in_footer = (bool) apply_filters( 'gtm4wp_' . GTM4WP_OPTION_EVENTS_FORMMOVE, true );
wp_enqueue_script( 'gtm4wp-form-move-tracker', $gtp4wp_script_path . 'gtm4wp-form-move-tracker.js', array(), GTM4WP_VERSION, $in_footer );
}
if ( $gtm4wp_options[ GTM4WP_OPTION_EVENTS_YOUTUBE ] ) {
require_once dirname( __FILE__ ) . '/../integration/youtube.php';
}
if ( $gtm4wp_options[ GTM4WP_OPTION_EVENTS_VIMEO ] ) {
require_once dirname( __FILE__ ) . '/../integration/vimeo.php';
}
if ( $gtm4wp_options[ GTM4WP_OPTION_EVENTS_SOUNDCLOUD ] ) {
require_once dirname( __FILE__ ) . '/../integration/soundcloud.php';
}
if ( $gtm4wp_options[ GTM4WP_OPTION_SCROLLER_ENABLED ] ) {
$in_footer = (bool) apply_filters( 'gtm4wp_' . GTM4WP_OPTION_SCROLLER_ENABLED, false );
wp_enqueue_script( 'gtm4wp-scroll-tracking', $gtp4wp_script_path . 'analytics-talk-content-tracking.js', array(), GTM4WP_VERSION, $in_footer );
}
wp_register_script( 'gtm4wp-additional-datalayer-pushes', '', array(), GTM4WP_VERSION, true );
wp_enqueue_script( 'gtm4wp-additional-datalayer-pushes' );
gtm4wp_fire_additional_datalayer_pushes();
}
/**
* Function executed during wp_footer.
* Inserts the GTM noscript/iframe code if code placement set to Footer.
* Outputs scripts that fire GTM tags on new user registration and login.
*
* @see https://developer.wordpress.org/reference/hooks/wp_footer/
*
* @return void
*/
function gtm4wp_wp_footer() {
global $gtm4wp_options, $gtm4wp_datalayer_name;
if ( GTM4WP_PLACEMENT_FOOTER === $gtm4wp_options[ GTM4WP_OPTION_GTM_PLACEMENT ] ) {
gtm4wp_the_gtm_tag();
}
if ( $gtm4wp_options[ GTM4WP_OPTION_EVENTS_USERLOGIN ] ) {
$user_logged_in = array_key_exists( 'gtm4wp_user_logged_in', $_COOKIE ) ?
filter_var( wp_unslash( $_COOKIE['gtm4wp_user_logged_in'] ), FILTER_VALIDATE_INT )
: 0;
if ( $user_logged_in ) {
$script_tag = '
' . gtm4wp_generate_script_opening_tag() . '
if ( window.' . esc_js( $gtm4wp_datalayer_name ) . ' ) {
window.' . esc_js( $gtm4wp_datalayer_name ) . ".push({
'event': 'gtm4wp.userLoggedIn'
});
}
";
echo htmlspecialchars_decode( //phpcs:ignore
wp_kses(
$script_tag,
gtm4wp_get_sanitize_script_block_rules()
)
);
unset( $_COOKIE['gtm4wp_user_logged_in'] );
}
}
if ( $gtm4wp_options[ GTM4WP_OPTION_EVENTS_NEWUSERREG ] ) {
$user_registered = array_key_exists( 'gtm4wp_user_registered', $_COOKIE ) ?
filter_var( wp_unslash( $_COOKIE['gtm4wp_user_registered'] ), FILTER_VALIDATE_INT )
: 0;
if ( $user_registered ) {
$script_tag = '
' . gtm4wp_generate_script_opening_tag() . '
if ( window.' . esc_js( $gtm4wp_datalayer_name ) . ' ) {
window.' . esc_js( $gtm4wp_datalayer_name ) . ".push({
'event': 'gtm4wp.userRegistered'
});
}
";
echo htmlspecialchars_decode( //phpcs:ignore
wp_kses(
$script_tag,
gtm4wp_get_sanitize_script_block_rules()
)
);
unset( $_COOKIE['gtm4wp_user_registered'] );
}
}
}
/**
* Function executed during wp_body_open, genesis_before, generate_before_header,
* elementor/page_templates/canvas/before_content, ct_before_builder or fl_before_builder.
*
* Outputs the noscript/iframe container code.
*
* @see https://developer.wordpress.org/reference/functions/wp_body_open/
* @see https://docs.generatepress.com/article/generate_before_header/
*
* @return void
*/
function gtm4wp_wp_body_open() {
global $gtm4wp_options;
if ( ( GTM4WP_PLACEMENT_BODYOPEN === $gtm4wp_options[ GTM4WP_OPTION_GTM_PLACEMENT ] ) || ( GTM4WP_PLACEMENT_BODYOPEN_AUTO === $gtm4wp_options[ GTM4WP_OPTION_GTM_PLACEMENT ] ) ) {
gtm4wp_the_gtm_tag();
}
}
/**
* Function executed during wp_head with high priority.
* Outputs some global JavaScript variables that needs to be accessable by other parts of the plugin.
*
* @see https://developer.wordpress.org/reference/functions/wp_head/
*
* @param boolean $echo If set to true and AMP is currently generating the page content, the HTML is outputed immediately.
* @return string|void Returns the HTML if the $echo parameter is set to false or when not AMP page generation is running.
*/
function gtm4wp_wp_header_top( $echo = true ) {
global $gtm4wp_options, $gtm4wp_datalayer_name;
$has_html5_support = current_theme_supports( 'html5' );
$add_cookiebot_ignore = (bool) $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_COOKIEBOT ];
// the data layer initialization has to use 'var' instead of 'let' since 'let' can break related browser extension and 3rd party script.
$_gtm_top_content = '
' . gtm4wp_generate_script_opening_tag() . '
var gtm4wp_datalayer_name = "' . esc_js( $gtm4wp_datalayer_name ) . '";
var ' . esc_js( $gtm4wp_datalayer_name ) . ' = ' . esc_js( $gtm4wp_datalayer_name ) . ' || [];';
// Load in the global variables from gtm4wp_add_global_vars_array / GTM4WP_WPFILTER_ADDGLOBALVARS_ARRAY filter.
$added_global_js_vars = (array) apply_filters( GTM4WP_WPFILTER_ADDGLOBALVARS_ARRAY, array() );
foreach ( $added_global_js_vars as $js_var_name => $js_var_value ) {
if ( is_string( $js_var_value ) ) {
$js_var_value = "'" . esc_js( $js_var_value ) . "'";
}
if ( is_bool( $js_var_value ) || ( empty( $js_var_value ) && ( 0 !== $js_var_value ) ) ) {
$js_var_value = $js_var_value ? 'true' : 'false';
}
if ( is_array( $js_var_value ) ) {
$js_var_value = wp_json_encode( $js_var_value );
}
if ( is_null( $js_var_value ) ) {
$js_var_value = 'null';
}
$_gtm_top_content .= '
const ' . esc_js( $js_var_name ) . ' = ' . $js_var_value . ';';
}
if ( $gtm4wp_options[ GTM4WP_OPTION_SCROLLER_ENABLED ] ) {
$_gtm_top_content .= '
const gtm4wp_scrollerscript_debugmode = ' . ( $gtm4wp_options[ GTM4WP_OPTION_SCROLLER_DEBUGMODE ] ? 'true' : 'false' ) . ';
const gtm4wp_scrollerscript_callbacktime = ' . (int) $gtm4wp_options[ GTM4WP_OPTION_SCROLLER_CALLBACKTIME ] . ';
const gtm4wp_scrollerscript_readerlocation = ' . (int) $gtm4wp_options[ GTM4WP_OPTION_SCROLLER_DISTANCE ] . ';
const gtm4wp_scrollerscript_contentelementid = "' . esc_js( $gtm4wp_options[ GTM4WP_OPTION_SCROLLER_CONTENTID ] ) . '";
const gtm4wp_scrollerscript_scannertime = ' . (int) $gtm4wp_options[ GTM4WP_OPTION_SCROLLER_READERTIME ] . ';';
}
if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WEBTOFFEE_GDPR ] ) {
$_gtm_top_content .= '
var CookieLawInfo_Accept_Callback = (function() {
var gtm4wp_original_cli_callback = CookieLawInfo_Accept_Callback;
return function() {
if ( !window.CLI.consent ) {
return false;
}
window.' . esc_js( $gtm4wp_datalayer_name ) . ' = window.' . esc_js( $gtm4wp_datalayer_name ) . ' || [];
window.' . esc_js( $gtm4wp_datalayer_name ) . '.push({
"event": "cookie_consent_update",
"consent_data": window.CLI.consent
});
for(var i in window.CLI.consent) {
window.' . esc_js( $gtm4wp_datalayer_name ) . '.push({
"event": "cookie_consent_" + i
});
}
if ( "function" == typeof gtm4wp_original_cli_callback ) {
gtm4wp_original_cli_callback();
}
}
})();';
}
$_gtm_top_content .= '
';
if ( ! gtm4wp_amp_running() ) {
if ( $echo ) {
echo wp_kses(
$_gtm_top_content,
array(
'script' => array(
'data-cfasync' => array(),
'data-pagespeed-no-defer' => array(),
'data-cookieconsent' => array(),
),
)
);
} else {
return $_gtm_top_content;
}
}
}
/**
* Function executed during wp_head.
* Returns the value of the consent mode flag.
*
* @see https://developer.wordpress.org/reference/functions/wp_head/
*
* @param string $flag The flag to be set.
* @return string The value of the flag (granted or denied).
*/
function gtm4wp_get_consent_mode_flag( $flag ) {
global $gtm4wp_options;
$flag_value = false;
if ( in_array(
$flag,
array(
GTM4WP_OPTION_INTEGRATE_CONSENTMODE_ADS,
GTM4WP_OPTION_INTEGRATE_CONSENTMODE_AD_USER_DATA,
GTM4WP_OPTION_INTEGRATE_CONSENTMODE_AD_PERSO,
GTM4WP_OPTION_INTEGRATE_CONSENTMODE_ANALYTICS,
GTM4WP_OPTION_INTEGRATE_CONSENTMODE_PERSO,
GTM4WP_OPTION_INTEGRATE_CONSENTMODE_FUNC,
GTM4WP_OPTION_INTEGRATE_CONSENTMODE_SECURUTY,
),
true
) ) {
if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_CONSENTMODE ] ) {
$flag_value = (bool) $gtm4wp_options[ $flag ];
/**
* Filter to overwrite the value of the consent mode flag.
* Should use boolean true or false. Returned value will be converted to
* string "granted" or "denied" afterwards.
*
* @since 1.22
*
* @param boolean $flag_value The value of the flag (boolean true or false).
* @param string $flag The flag to be set.
*
* @return boolean The updated value of the flag (boolean true or false).
*/
$flag_value = apply_filters( GTM4WP_WPFILTER_OVERWRITE_COMO_FLAG, $flag_value, $flag );
}
}
return ( $flag_value ? 'granted' : 'denied' );
}
/**
* Function executed during wp_head.
* Outputs the main Google Tag Manager container code.
*
* @see https://developer.wordpress.org/reference/functions/wp_head/
*
* @param boolean $echo If set to true and AMP is currently generating the page content, the HTML is outputed immediately.
* @return string|void Returns the HTML if the $echo parameter is set to false or when not AMP page generation is running.
*/
function gtm4wp_wp_header_begin( $echo = true ) {
global $gtm4wp_datalayer_name, $gtm4wp_datalayer_data, $gtm4wp_options;
$no_console_log = (bool) $gtm4wp_options[ GTM4WP_OPTION_NOCONSOLELOG ];
$script_tag = '
' . gtm4wp_generate_script_opening_tag();
if ( '' !== $gtm4wp_options[ GTM4WP_OPTION_GTM_CODE ] ) {
$gtm4wp_datalayer_data = array();
$gtm4wp_datalayer_data = (array) apply_filters( GTM4WP_WPFILTER_COMPILE_DATALAYER, $gtm4wp_datalayer_data );
$script_tag .= '
var dataLayer_content = ' . wp_json_encode( $gtm4wp_datalayer_data, JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK ) . ';';
$script_tag .= '
' . esc_js( $gtm4wp_datalayer_name ) . '.push( dataLayer_content );';
}
$script_tag .= '
';
echo htmlspecialchars_decode( //phpcs:ignore
wp_kses(
$script_tag,
gtm4wp_get_sanitize_script_block_rules()
)
);
do_action( GTM4WP_WPACTION_AFTER_DATALAYER );
$output_container_code = true;
if ( GTM4WP_PLACEMENT_OFF === $gtm4wp_options[ GTM4WP_OPTION_GTM_PLACEMENT ] ) {
$output_container_code = false;
}
if ( ! $no_console_log && ! $output_container_code ) {
$script_tag = '
' . gtm4wp_generate_script_opening_tag() . '
console.warn && console.warn("[GTM4WP] Google Tag Manager container code placement set to OFF !!!");
console.warn && console.warn("[GTM4WP] Data layer codes are active but GTM container must be loaded using custom coding !!!");
';
echo htmlspecialchars_decode( //phpcs:ignore
wp_kses(
$script_tag,
gtm4wp_get_sanitize_script_block_rules()
)
);
}
$disabled_roles = explode( ',', (string) $gtm4wp_options[ GTM4WP_OPTION_NOGTMFORLOGGEDIN ] );
if ( count( $disabled_roles ) > 0 ) {
$current_user = wp_get_current_user();
foreach ( $current_user->roles as $user_role ) {
if ( in_array( $user_role, $disabled_roles, true ) ) {
$output_container_code = false;
if ( ! $no_console_log ) {
$script_tag = '
' . gtm4wp_generate_script_opening_tag() . '
console.warn && console.warn("[GTM4WP] Google Tag Manager container code was disabled for this user role: ' . esc_js( $user_role ) . ' !!!");
console.warn && console.warn("[GTM4WP] Logout or login with a user having a different user role!");
console.warn && console.warn("[GTM4WP] Data layer codes are active but GTM container code is omitted !!!");
';
echo htmlspecialchars_decode( //phpcs:ignore
wp_kses(
$script_tag,
gtm4wp_get_sanitize_script_block_rules()
)
);
}
break;
}
}
}
if ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_CONSENTMODE ] ) {
$script_tag = '
' . gtm4wp_generate_script_opening_tag() . '
if (typeof gtag == "undefined") {
function gtag(){dataLayer.push(arguments);}
}
gtag("consent", "default", {
"analytics_storage": "' . gtm4wp_get_consent_mode_flag( GTM4WP_OPTION_INTEGRATE_CONSENTMODE_ANALYTICS ) . '",
"ad_storage": "' . gtm4wp_get_consent_mode_flag( GTM4WP_OPTION_INTEGRATE_CONSENTMODE_ADS ) . '",
"ad_user_data": "' . gtm4wp_get_consent_mode_flag( GTM4WP_OPTION_INTEGRATE_CONSENTMODE_AD_USER_DATA ) . '",
"ad_personalization": "' . gtm4wp_get_consent_mode_flag( GTM4WP_OPTION_INTEGRATE_CONSENTMODE_AD_PERSO ) . '",
"functionality_storage": "' . gtm4wp_get_consent_mode_flag( GTM4WP_OPTION_INTEGRATE_CONSENTMODE_FUNC ) . '",
"security_storage": "' . gtm4wp_get_consent_mode_flag( GTM4WP_OPTION_INTEGRATE_CONSENTMODE_SECURUTY ) . '",
"personalization_storage": "' . gtm4wp_get_consent_mode_flag( GTM4WP_OPTION_INTEGRATE_CONSENTMODE_PERSO ) . '",
});
';
echo htmlspecialchars_decode( //phpcs:ignore
wp_kses(
$script_tag,
gtm4wp_get_sanitize_script_block_rules()
)
);
}
if ( ( '' !== $gtm4wp_options[ GTM4WP_OPTION_GTM_CODE ] ) && $output_container_code ) {
$_gtm_codes = explode( ',', str_replace( array( ';', ' ' ), array( ',', '' ), $gtm4wp_options[ GTM4WP_OPTION_GTM_CODE ] ) );
$_gtm_tag = '';
foreach ( $_gtm_codes as $one_gtm_id ) {
if ( ! preg_match( '/^GTM-[A-Z0-9]+$/', $one_gtm_id ) ) {
continue;
}
// for PHP 7- compatibility.
if ( ! defined( 'FILTER_FLAG_HOSTNAME' ) ) {
define( 'FILTER_FLAG_HOSTNAME', 0 );
}
$_gtm_domain_test = ( '' === $gtm4wp_options[ GTM4WP_OPTION_GTMDOMAIN ] ) ? 'www.googletagmanager.com' : strtolower( $gtm4wp_options[ GTM4WP_OPTION_GTMDOMAIN ] );
$_gtm_domain_name = filter_var( $_gtm_domain_test, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME );
if ( false === $_gtm_domain_name ) {
$_gtm_domain_name = 'www.googletagmanager.com';
}
$_gtm_domain_path = ( '' === $gtm4wp_options[ GTM4WP_OPTION_GTMCUSTOMPATH ] ) ? 'gtm.js' : $gtm4wp_options[ GTM4WP_OPTION_GTMCUSTOMPATH ];
$_gtm_custom_path_has_error = (bool) preg_match( '/^[a-zA-Z0-9\.\-\_\/]+$/', $_gtm_domain_path );
if ( false === $_gtm_custom_path_has_error ) {
$_gtm_domain_path = 'gtm.js';
}
$script_tag = '
' . gtm4wp_generate_script_opening_tag() . '
(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_domain_name ) . '/' . esc_js( $_gtm_domain_path ) . '?id=\'+i+dl' .
( ( ( '' !== $gtm4wp_options[ GTM4WP_OPTION_ENV_GTM_AUTH ] ) && ( '' !== $gtm4wp_options[ GTM4WP_OPTION_ENV_GTM_PREVIEW ] ) ) ? "+'>m_auth=" . esc_attr( $gtm4wp_options[ GTM4WP_OPTION_ENV_GTM_AUTH ] ) . '>m_preview=' . esc_attr( $gtm4wp_options[ GTM4WP_OPTION_ENV_GTM_PREVIEW ] ) . ">m_cookies_win=x'" : '' ) . ';f.parentNode.insertBefore(j,f);
})(window,document,\'script\',\'' . esc_js( $gtm4wp_datalayer_name ) . '\',\'' . esc_js( $one_gtm_id ) . '\');
';
echo htmlspecialchars_decode( //phpcs:ignore
wp_kses(
$script_tag,
gtm4wp_get_sanitize_script_block_rules()
)
);
} // end foreach $_gtm_codes
} // end if container code output possible
do_action( GTM4WP_WPACTION_AFTER_CONTAINER_CODE );
echo '
';
}
/**
* Function executed during wp_login.
* Sets a cookie so that the next page load can fire a GTM event after a user has been logged in.
*
* @see https://developer.wordpress.org/reference/hooks/wp_login/
*
* @return void
*/
function gtm4wp_wp_login() {
setcookie(
'gtm4wp_user_logged_in',
'1',
0,
'/',
'',
( false !== strstr( get_option( 'home' ), 'https:' ) ) && is_ssl(),
true
);
}
/**
* Function executed during user_register.
* Sets a cookie so that the next page load can fire a GTM event after a new user has been registered.
*
* @see https://developer.wordpress.org/reference/hooks/user_register/
*
* @return void
*/
function gtm4wp_user_register() {
setcookie(
'gtm4wp_user_registered',
'1',
0,
'/',
'',
( false !== strstr( get_option( 'home' ), 'https:' ) ) && is_ssl(),
true
);
}
/**
* Function executed during rocket_excluded_inline_js_content if WP-Rocket is active.
* Excludes the dataLayer variable and gtm4wp* variables from being combined into the minified JS file.
*
* @param array $pattern Patterns to match in inline JS content.
* @return array Patterns to match in inline JS content extended with additional items.
*/
function gtm4wp_rocket_excluded_inline_js_content( $pattern ) {
$pattern[] = 'dataLayer';
$pattern[] = 'gtm4wp';
return $pattern;
}
/**
* Function executed during init.
* Removes the cookies set to fire GTM events for new user registration and user login.
*
* @see https://developer.wordpress.org/reference/hooks/init/
*
* @return void
*/
function gtm4wp_wp_init() {
if ( array_key_exists( 'gtm4wp_user_logged_in', $_COOKIE ) ) {
setcookie(
'gtm4wp_user_logged_in',
'',
-10000,
'/',
'',
( false !== strstr( get_option( 'home' ), 'https:' ) ) && is_ssl(),
true
);
}
if ( array_key_exists( 'gtm4wp_user_registered', $_COOKIE ) ) {
setcookie(
'gtm4wp_user_registered',
'',
-10000,
'/',
'',
( false !== strstr( get_option( 'home' ), 'https:' ) ) && is_ssl(),
true
);
}
}
/**
* Outputs the necessary JavaScript codes to fire additional data layer events just after the main GTM container code.
*
* @return void
*/
function gtm4wp_fire_additional_datalayer_pushes() {
global $gtm4wp_options, $gtm4wp_datalayer_name, $gtm4wp_additional_datalayer_pushes;
$has_html5_support = current_theme_supports( 'html5' );
$add_cookiebot_ignore = (bool) $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_COOKIEBOT ];
foreach ( $gtm4wp_additional_datalayer_pushes as $one_event ) {
$datalayer_push_code = '';
if ( array_key_exists( 'js_before', $one_event ) ) {
$datalayer_push_code .= $one_event['js_before'];
}
if ( array_key_exists( 'datalayer_object', $one_event ) ) {
$datalayer_push_code .= '
' . esc_js( $gtm4wp_datalayer_name ) . '.push(' . wp_json_encode( $one_event['datalayer_object'], JSON_UNESCAPED_UNICODE ) . ');';
}
if ( array_key_exists( 'js_after', $one_event ) ) {
$datalayer_push_code .= $one_event['js_after'];
}
wp_add_inline_script( 'gtm4wp-additional-datalayer-pushes', $datalayer_push_code, 'after' );
}
// Reset array so that additional data can be added and this function can be re-run without double running code.
$gtm4wp_additional_datalayer_pushes = array();
}
/**
* Queuenes a data layer event to be fired after the main GTM container code.
*
* @param string $event_name The name of the GTM event.
* @param array $event_data Additional event parameters to be passed after the event. Optional.
* @param string $js_before Inline JS code to be added before the dataLayer.push() line.
* @param string $js_after Inline JS code to be added after the dataLayer.push() line.
* @return bool Returns true when data layer event was successfully queued to fire after the main GTM container code. Returns false when function parameter types are invalid.
*/
function gtm4wp_datalayer_push( $event_name, $event_data = array(), $js_before = '', $js_after = '' ) {
global $gtm4wp_additional_datalayer_pushes;
if ( ! is_string( $event_name ) ) {
return false;
}
if ( ! is_array( $event_data ) ) {
return false;
}
$gtm4wp_additional_datalayer_pushes[] = array(
'datalayer_object' => array_merge(
$event_data,
array(
'event' => $event_name,
)
),
'js_before' => $js_before,
'js_after' => $js_after,
);
return true;
}
add_action( 'wp_enqueue_scripts', 'gtm4wp_enqueue_scripts' );
$gtm4wp_header_begin_prior = 10;
if ( isset( $GLOBALS['gtm4wp_options'] ) && $GLOBALS['gtm4wp_options'][ GTM4WP_OPTION_LOADEARLY ] ) {
$gtm4wp_header_begin_prior = 2;
}
add_action( 'wp_head', 'gtm4wp_wp_header_begin', $gtm4wp_header_begin_prior, 0 );
add_action( 'wp_head', 'gtm4wp_wp_header_top', 1, 0 );
add_action( 'wp_footer', 'gtm4wp_wp_footer' );
add_action( 'wp_loaded', 'gtm4wp_wp_loaded' );
add_filter( GTM4WP_WPFILTER_COMPILE_DATALAYER, 'gtm4wp_add_basic_datalayer_data' );
add_action( 'init', 'gtm4wp_wp_init' );
// to be able to easily migrate from other Google Tag Manager plugins.
add_action( 'body_open', 'gtm4wp_wp_body_open' );
// compatibility with existing themes that natively support code injection after opening body tag.
add_action( 'genesis_before', 'gtm4wp_wp_body_open' ); // Genisis theme.
add_action( 'generate_before_header', 'gtm4wp_wp_body_open', 0 ); // GeneratePress theme.
add_action( 'elementor/page_templates/canvas/before_content', 'gtm4wp_wp_body_open' ); // Elementor.
add_action( 'ct_before_builder', 'gtm4wp_wp_body_open', 0 ); // Oxygen Builder.
add_action( 'fl_before_builder', 'gtm4wp_wp_body_open', 0 ); // Beaver Builder Theme.
// standard WP theme support for body open tags.
add_action( 'wp_body_open', 'gtm4wp_wp_body_open' );
add_filter( 'rocket_excluded_inline_js_content', 'gtm4wp_rocket_excluded_inline_js_content' ); // WP Rocket.
// only activate WooCommerce integration for minimum supported WooCommerce version.
if (
isset( $GLOBALS['gtm4wp_options'] )
&& $GLOBALS['gtm4wp_options'][ GTM4WP_OPTION_INTEGRATE_WCTRACKECOMMERCE ]
&& isset( $GLOBALS['woocommerce'] )
&& version_compare( WC()->version, '5.0', '>=' )
) {
require_once dirname( __FILE__ ) . '/../integration/woocommerce.php';
}
if ( isset( $GLOBALS['gtm4wp_options'] ) && ( $GLOBALS['gtm4wp_options'][ GTM4WP_OPTION_EVENTS_USERLOGIN ] ) ) {
add_action( 'wp_login', 'gtm4wp_wp_login' );
}
if ( isset( $GLOBALS['gtm4wp_options'] ) && ( $GLOBALS['gtm4wp_options'][ GTM4WP_OPTION_EVENTS_NEWUSERREG ] ) ) {
add_action( 'user_register', 'gtm4wp_user_register' );
}