gtm4wp_wp_header_begin

The Google Tag Manager for WordPress gtm4wp wp header begin function.

Description

gtm4wp_wp_header_begin(); 

Usage

  1. if ( !function_exists( 'gtm4wp_wp_header_begin' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'google-tag-manager-for-wordpress/public/frontend.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = gtm4wp_wp_header_begin(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/public/frontend.php  
  1. function gtm4wp_wp_header_begin() { 
  2. global $gtm4wp_datalayer_name, $gtm4wp_options; 
  3.  
  4. $_gtm_header_content = ' 
  5. <!-- Google Tag Manager for WordPress by DuracellTomi - http://duracelltomi.com --> 
  6. <script data-cfasync="false" type="text/javascript"> 
  7. var gtm4wp_datalayer_name = "' . $gtm4wp_datalayer_name . '"; 
  8. var ' . $gtm4wp_datalayer_name . ' = ' . $gtm4wp_datalayer_name . ' || [];'; 
  9.  
  10. if ( $gtm4wp_options[ GTM4WP_OPTION_SCROLLER_ENABLED ] ) { 
  11. $_gtm_header_content .= ' 
  12.  
  13. var gtm4wp_scrollerscript_debugmode = ' . ( $gtm4wp_options[ GTM4WP_OPTION_SCROLLER_DEBUGMODE ] ? 'true' : 'false' ) . '; 
  14. var gtm4wp_scrollerscript_callbacktime = ' . (int) $gtm4wp_options[ GTM4WP_OPTION_SCROLLER_CALLBACKTIME ] . '; 
  15. var gtm4wp_scrollerscript_readerlocation = ' . (int) $gtm4wp_options[ GTM4WP_OPTION_SCROLLER_DISTANCE ] . '; 
  16. var gtm4wp_scrollerscript_contentelementid = "' . $gtm4wp_options[ GTM4WP_OPTION_SCROLLER_CONTENTID ] . '"; 
  17. var gtm4wp_scrollerscript_scannertime = ' . (int) $gtm4wp_options[ GTM4WP_OPTION_SCROLLER_READERTIME ] . ';'; 
  18.  
  19. if ( $gtm4wp_options[ GTM4WP_OPTION_GTM_CODE ] != "" ) { 
  20. $gtm4wp_datalayer_data = array(); 
  21. $gtm4wp_datalayer_data = (array) apply_filters( GTM4WP_WPFILTER_COMPILE_DATALAYER, $gtm4wp_datalayer_data ); 
  22.  
  23. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_REMARKETING ] ) { 
  24. // add adwords remarketing tags as suggested here: 
  25. // https://support.google.com/tagmanager/answer/3002580?hl=en 
  26.  
  27. add_filter( GTM4WP_WPFILTER_COMPILE_REMARKTING, "gtm4wp_filter_visitor_keys" ); 
  28. $gtm4wp_remarketing_tags = (array) apply_filters( GTM4WP_WPFILTER_COMPILE_REMARKTING, $gtm4wp_datalayer_data ); 
  29.  
  30. $_gtm_header_content .= ' 
  31. var google_tag_params = ' . json_encode( $gtm4wp_remarketing_tags ) . ';'; 
  32. $gtm4wp_datalayer_data["google_tag_params"] = "-~-window.google_tag_params-~-"; 
  33.  
  34. if ( $gtm4wp_options[ GTM4WP_OPTION_EVENTS_DOWNLOADS ] ) { 
  35. $_gtm_header_content .= ' 
  36. jQuery( function() { 
  37. gtm4wp_track_downloads( "' . str_replace( '"', '', $gtm4wp_options[ GTM4WP_OPTION_EVENTS_DWLEXT ] ) . '" ); 
  38. });'; 
  39.  
  40. if ( version_compare( PHP_VERSION, '5.4.0' ) >= 0 ) { 
  41. $dl_json_data = json_encode( $gtm4wp_datalayer_data, JSON_UNESCAPED_UNICODE ); 
  42. } else { 
  43. $dl_json_data = json_encode( $gtm4wp_datalayer_data ); 
  44.  
  45. $_gtm_header_content .= ' 
  46. ' . $gtm4wp_datalayer_name . '.push(' . str_replace( 
  47. array( '"-~-', '-~-"' ),  
  48. array( "", "" ),  
  49. str_replace( "", "-", $dl_json_data ) 
  50. ) . ');'; 
  51.  
  52. $_gtm_header_content .= ' 
  53. </script>'; 
  54.  
  55. if ( ( $gtm4wp_options[ GTM4WP_OPTION_GTM_CODE ] != "" ) && ( GTM4WP_PLACEMENT_OFF != $gtm4wp_options[ GTM4WP_OPTION_GTM_PLACEMENT ] ) ) { 
  56. $_gtm_codes = explode( ", ", str_replace( array(";", " "), array(", ", ""), $gtm4wp_options[ GTM4WP_OPTION_GTM_CODE ] ) ); 
  57.  
  58. $_gtm_tag = ''; 
  59. foreach( $_gtm_codes as $one_gtm_code ) { 
  60. $_gtm_tag .= ' 
  61. <script data-cfasync="false">(function(w, d, s, l, i) {w[l]=w[l]||[];w[l].push({\'gtm.start\': 
  62. new Date().getTime(), event:\'gtm.js\'});var f=d.getElementsByTagName(s)[0],  
  63. j=d.createElement(s), dl=l!=\'dataLayer\'?\'&l=\'+l:\'\';j.async=true;j.src= 
  64. \'//www.googletagmanager.com/gtm.\''.'+\'js?id=\'+i+dl;f.parentNode.insertBefore(j, f); 
  65. })(window, document, \'script\', \'' . $gtm4wp_datalayer_name . '\', \'' . $one_gtm_code . '\');</script>'; 
  66.  
  67. $_gtm_tag .= ' 
  68. <!-- End Google Tag Manager -->'; 
  69.  
  70. $_gtm_tag = apply_filters( GTM4WP_WPFILTER_GETTHEGTMTAG, $_gtm_tag ); 
  71. $_gtm_header_content .= $_gtm_tag; 
  72.  
  73. $_gtm_header_content .= ' 
  74. <!-- End Google Tag Manager for WordPress by DuracellTomi -->'; 
  75.  
  76. echo $_gtm_header_content;