gtm4wp_add_basic_datalayer_data

The Google Tag Manager for WordPress gtm4wp add basic datalayer data function.

Description

gtm4wp_add_basic_datalayer_data( $dataLayer ); 

Parameters (1)

0. $datalayer
The datalayer.

Usage

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

Defined (1)

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

/public/frontend.php  
  1. function gtm4wp_add_basic_datalayer_data( $dataLayer ) { 
  2. global $wp_query, $gtm4wp_options; 
  3.  
  4. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_SITEID ] || $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_SITENAME ] ) { 
  5. $dataLayer["siteID"] = 0; 
  6. $dataLayer["siteName"] = ""; 
  7.  
  8. if ( function_exists( 'get_blog_details' ) ) { 
  9. $gtm4wp_blogdetails = get_blog_details(); 
  10.  
  11. $dataLayer["siteID"] = $gtm4wp_blogdetails->blog_id; 
  12. $dataLayer["siteName"] = $gtm4wp_blogdetails->blogname; 
  13.  
  14. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_LOGGEDIN ] ) { 
  15. if ( is_user_logged_in() ) { 
  16. $dataLayer["visitorLoginState"] = "logged-in"; 
  17. } else { 
  18. $dataLayer["visitorLoginState"] = "logged-out"; 
  19.  
  20. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_USERROLE ] || $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_USEREMAIL ] ) { 
  21. $current_user = wp_get_current_user(); 
  22.  
  23. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_USERROLE ] ) { 
  24. $dataLayer["visitorType"] = ( empty( $current_user->roles[0] ) ? "visitor-logged-out" : $current_user->roles[0] ); 
  25.  
  26. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_USEREMAIL ] ) { 
  27. $dataLayer["visitorEmail"] = ( empty( $current_user->user_email ) ? "" : $current_user->user_email ); 
  28.  
  29. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_USERID ] ) { 
  30. $_gtm4wp_userid = get_current_user_id(); 
  31. if ( $_gtm4wp_userid > 0 ) { 
  32. $dataLayer["visitorId"] = $_gtm4wp_userid; 
  33.  
  34. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_POSTTITLE ] ) { 
  35. $dataLayer["pageTitle"] = strip_tags( wp_title( "|", false, "right" ) ); 
  36.  
  37. if ( is_singular() ) { 
  38. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_POSTTYPE ] ) { 
  39. $dataLayer["pagePostType"] = get_post_type(); 
  40. $dataLayer["pagePostType2"] = "single-".get_post_type(); 
  41.  
  42. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_CATEGORIES ] ) { 
  43. $_post_cats = get_the_category(); 
  44. if ( $_post_cats ) { 
  45. $dataLayer["pageCategory"] = array(); 
  46. foreach( $_post_cats as $_one_cat ) { 
  47. $dataLayer["pageCategory"][] = $_one_cat->slug; 
  48.  
  49. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_TAGS ] ) { 
  50. $_post_tags = get_the_tags(); 
  51. if ( $_post_tags ) { 
  52. $dataLayer["pageAttributes"] = array(); 
  53. foreach( $_post_tags as $_one_tag ) { 
  54. $dataLayer["pageAttributes"][] = $_one_tag->slug; 
  55.  
  56. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_AUTHORID ] || $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_AUTHOR ] ) { 
  57. $postuser = get_userdata( $GLOBALS["post"]->post_author ); 
  58.  
  59. if ( false !== $postuser ) { 
  60. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_AUTHORID ] ) { 
  61. $dataLayer["pagePostAuthorID"] = $postuser->ID; 
  62.  
  63. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_AUTHOR ] ) { 
  64. $dataLayer["pagePostAuthor"] = $postuser->display_name; 
  65.  
  66. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_POSTDATE ] ) { 
  67. $dataLayer["pagePostDate"] = get_the_date(); 
  68. $dataLayer["pagePostDateYear"] = get_the_date( "Y" ); 
  69. $dataLayer["pagePostDateMonth"] = get_the_date( "m" ); 
  70. $dataLayer["pagePostDateDay"] = get_the_date( "d" ); 
  71.  
  72. if ( is_archive() || is_post_type_archive() ) { 
  73. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_POSTTYPE ] ) { 
  74. $dataLayer["pagePostType"] = get_post_type(); 
  75.  
  76. if ( is_category() ) { 
  77. $dataLayer["pagePostType2"] = "category-".get_post_type(); 
  78. } else if ( is_tag() ) { 
  79. $dataLayer["pagePostType2"] = "tag-".get_post_type(); 
  80. } else if ( is_tax() ) { 
  81. $dataLayer["pagePostType2"] = "tax-".get_post_type(); 
  82. } else if ( is_author() ) { 
  83. $dataLayer["pagePostType2"] = "author-".get_post_type(); 
  84. } else if ( is_year() ) { 
  85. $dataLayer["pagePostType2"] = "year-".get_post_type(); 
  86.  
  87. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_POSTDATE ] ) { 
  88. $dataLayer["pagePostDateYear"] = get_the_date( "Y" ); 
  89. } else if ( is_month() ) { 
  90. $dataLayer["pagePostType2"] = "month-".get_post_type(); 
  91.  
  92. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_POSTDATE ] ) { 
  93. $dataLayer["pagePostDateYear"] = get_the_date( "Y" ); 
  94. $dataLayer["pagePostDateMonth"] = get_the_date( "m" ); 
  95. } else if ( is_day() ) { 
  96. $dataLayer["pagePostType2"] = "day-".get_post_type(); 
  97.  
  98. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_POSTDATE ] ) { 
  99. $dataLayer["pagePostDate"] = get_the_date(); 
  100. $dataLayer["pagePostDateYear"] = get_the_date( "Y" ); 
  101. $dataLayer["pagePostDateMonth"] = get_the_date( "m" ); 
  102. $dataLayer["pagePostDateDay"] = get_the_date( "d" ); 
  103. } else if ( is_time() ) { 
  104. $dataLayer["pagePostType2"] = "time-".get_post_type(); 
  105. } else if ( is_date() ) { 
  106. $dataLayer["pagePostType2"] = "date-".get_post_type(); 
  107.  
  108. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_POSTDATE ] ) { 
  109. $dataLayer["pagePostDate"] = get_the_date(); 
  110. $dataLayer["pagePostDateYear"] = get_the_date( "Y" ); 
  111. $dataLayer["pagePostDateMonth"] = get_the_date( "m" ); 
  112. $dataLayer["pagePostDateDay"] = get_the_date( "d" ); 
  113.  
  114. if ( ( is_tax() || is_category() ) && $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_CATEGORIES ] ) { 
  115. $_post_cats = get_the_category(); 
  116. $dataLayer["pageCategory"] = array(); 
  117. foreach( $_post_cats as $_one_cat ) { 
  118. $dataLayer["pageCategory"][] = $_one_cat->slug; 
  119.  
  120. if ( ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_AUTHORID ] ) && ( is_author() ) ) { 
  121. global $authordata; 
  122. $dataLayer["pagePostAuthorID"] = isset( $authordata->ID ) ? $authordata->ID : 0; 
  123.  
  124. if ( ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_AUTHOR ] ) && ( is_author() ) ) { 
  125. $dataLayer["pagePostAuthor"] = get_the_author(); 
  126.  
  127. if ( is_search() ) { 
  128. $dataLayer["siteSearchTerm"] = get_search_query(); 
  129. $dataLayer["siteSearchFrom"] = ( isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : "" ); 
  130. $dataLayer["siteSearchResults"] = $wp_query->post_count; 
  131.  
  132. if ( is_front_page() && $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_POSTTYPE ] ) { 
  133. $dataLayer["pagePostType"] = "frontpage"; 
  134.  
  135. if ( !is_front_page() && is_home() && $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_POSTTYPE ] ) { 
  136. $dataLayer["pagePostType"] = "bloghome"; 
  137.  
  138. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_BROWSERDATA ] || $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_OSDATA ] || $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_DEVICEDATA ] ) { 
  139. spl_autoload_register( function( $class ) { 
  140. $class_parts = explode( "\\", $class ); 
  141. if ( "WhichBrowser" == $class_parts[0] ) { 
  142. include dirname( __FILE__ ) . "/../integration/whichbrowser/" . str_replace( array( "WhichBrowser", "\\" ), array( "src", "/" ), $class ) . ".php"; 
  143. }); 
  144.  
  145. require_once( dirname( __FILE__ ) . "/../integration/whichbrowser/src/Parser.php" ); 
  146.  
  147. $gtp4wp_headers = getallheaders(); 
  148. if ( ( false === $gtp4wp_headers ) && isset( $_SERVER['HTTP_USER_AGENT'] ) ) { 
  149. $gtp4wp_headers = $_SERVER['HTTP_USER_AGENT']; 
  150. if ( false !== $gtp4wp_headers ) { 
  151. $detected = new WhichBrowser\Parser($gtp4wp_headers); 
  152.  
  153. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_BROWSERDATA ] ) { 
  154. $dataLayer["browserName"] = isset( $detected->browser->name ) ? $detected->browser->name : ""; 
  155. $dataLayer["browserVersion"] = isset( $detected->browser->version->value ) ? $detected->browser->version->value : ""; 
  156.  
  157. $dataLayer["browserEngineName"] = isset( $detected->engine->name ) ? $detected->engine->name : ""; 
  158. $dataLayer["browserEngineVersion"] = isset( $detected->engine->version->value ) ? $detected->engine->version->value : ""; 
  159.  
  160. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_OSDATA ] ) { 
  161. $dataLayer["osName"] = isset( $detected->os->name ) ? $detected->os->name : ""; 
  162. $dataLayer["osVersion"] = isset( $detected->os->version->value ) ? $detected->os->version->value : ""; 
  163.  
  164. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_DEVICEDATA ] ) { 
  165. $dataLayer["deviceType"] = isset( $detected->device->type ) ? $detected->device->type : ""; 
  166. $dataLayer["deviceManufacturer"] = isset( $detected->device->manufacturer ) ? $detected->device->manufacturer : ""; 
  167. $dataLayer["deviceModel"] = isset( $detected->device->model ) ? $detected->device->model : ""; 
  168.  
  169. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_POSTCOUNT ] ) { 
  170. $dataLayer["postCountOnPage"] = (int) $wp_query->post_count; 
  171. $dataLayer["postCountTotal"] = (int) $wp_query->found_posts; 
  172.  
  173. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_POSTID ] && is_singular() === true) { 
  174. $dataLayer["postID"] = (int) get_the_ID(); 
  175.  
  176. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_ENABLE ] > 0 ) { 
  177. $_gtmrestrictlistitems = array(); 
  178.  
  179. // IDs from https://developers.google.com/tag-manager/devguide#security 
  180. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_ADADVISOR ] ) { 
  181. $_gtmrestrictlistitems[] = "ta"; 
  182. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_ADROLL ] ) { 
  183. $_gtmrestrictlistitems[] = "asp"; 
  184. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_AWCONV ] ) { 
  185. $_gtmrestrictlistitems[] = "awct"; 
  186. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_AWREMARKET ] ) { 
  187. $_gtmrestrictlistitems[] = "sp"; 
  188. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_BIZO ] ) { 
  189. $_gtmrestrictlistitems[] = "bzi"; 
  190. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_CLICKTALE ] ) { 
  191. $_gtmrestrictlistitems[] = "cts"; 
  192. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_COMSCORE ] ) { 
  193. $_gtmrestrictlistitems[] = "csm"; 
  194. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_CUSTOMHTML ] ) { 
  195. $_gtmrestrictlistitems[] = "html"; 
  196. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_CUSTOMIMG ] ) { 
  197. $_gtmrestrictlistitems[] = "img"; 
  198. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_DBLCLKCOUNT ] ) { 
  199. $_gtmrestrictlistitems[] = "flc"; 
  200. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_DBLCLKSALES ] ) { 
  201. $_gtmrestrictlistitems[] = "fls"; 
  202. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_GACLASSIC ] ) { 
  203. $_gtmrestrictlistitems[] = "ga"; 
  204. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_MARIN ] ) { 
  205. $_gtmrestrictlistitems[] = "ms"; 
  206. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_MPLEXIFRAME ] ) { 
  207. $_gtmrestrictlistitems[] = "mpm"; 
  208. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_MPLEXROI ] ) { 
  209. $_gtmrestrictlistitems[] = "mpr"; 
  210. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_MEDIA6DEG ] ) { 
  211. $_gtmrestrictlistitems[] = "m6d"; 
  212. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_TURNCONV ] ) { 
  213. $_gtmrestrictlistitems[] = "tc"; 
  214. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_TURNDATA ] ) { 
  215. $_gtmrestrictlistitems[] = "tdc"; 
  216. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_UA ] ) { 
  217. $_gtmrestrictlistitems[] = "ua"; 
  218.  
  219. $_gtmwhitelist = array(); 
  220. $_gtmblacklist = array(); 
  221. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_ENABLE ] == 1 ) { 
  222. $_gtmblacklist = array_merge($_gtmblacklist, $_gtmrestrictlistitems); 
  223. } else { 
  224. $_gtmwhitelist = array_merge($_gtmwhitelist, $_gtmrestrictlistitems); 
  225.  
  226. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_MACRO_DOMELEMENT ] ) { 
  227. $_gtmwhitelist[] = "d"; 
  228.  
  229. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_MACRO_CUSTOMJS ] ) { 
  230. $_gtmwhitelist[] = "jsm"; 
  231.  
  232. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_MACRO_CONSTANT ] ) { 
  233. $_gtmwhitelist[] = "c"; 
  234.  
  235. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_MACRO_1STCOOKIE ] ) { 
  236. $_gtmwhitelist[] = "k"; 
  237.  
  238. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_MACRO_EVENTNAME ] ) { 
  239. $_gtmwhitelist[] = "e"; 
  240.  
  241. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_MACRO_JSVAR ] ) { 
  242. $_gtmwhitelist[] = "j"; 
  243.  
  244. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_MACRO_DLAYERVAR ] ) { 
  245. $_gtmwhitelist[] = "v"; 
  246.  
  247. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_MACRO_RANDOMNUM ] ) { 
  248. $_gtmwhitelist[] = "r"; 
  249.  
  250. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_MACRO_REFERRER ] ) { 
  251. $_gtmwhitelist[] = "f"; 
  252.  
  253. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_MACRO_URL ] ) { 
  254. $_gtmwhitelist[] = "u"; 
  255.  
  256. if ( $gtm4wp_options[ GTM4WP_OPTION_BLACKLIST_MACRO_AUTOEVENT ] ) { 
  257. $_gtmwhitelist[] = "v"; 
  258.  
  259. $dataLayer[ "gtm.whitelist" ] = $_gtmwhitelist; 
  260. $dataLayer[ "gtm.blacklist" ] = $_gtmblacklist; 
  261.  
  262. if ( $gtm4wp_options[ GTM4WP_OPTION_INCLUDE_WEATHER ] ) { 
  263. $dataLayer[ "weatherCategory" ] = __( "(no weather data available)", 'duracelltomi-google-tag-manager' ); 
  264. $dataLayer[ "weatherDescription" ] = __( "(no weather data available)", 'duracelltomi-google-tag-manager' ); 
  265. $dataLayer[ "weatherTemp" ] = 0; 
  266. $dataLayer[ "weatherPressure" ] = 0; 
  267. $dataLayer[ "weatherWindSpeed" ] = 0; 
  268. $dataLayer[ "weatherWindDeg" ] = 0; 
  269.  
  270. $gtm4wp_sessionid = array_key_exists( "gtm4wp_sessoionid", $_COOKIE ) ? $_COOKIE[ "gtm4wp_sessoionid" ] : ""; 
  271. // this is needed so that nobody can do a hack by editing our cookie 
  272. $gtm4wp_sessionid = str_replace( "'", "", trim( basename( $gtm4wp_sessionid ) ) ); 
  273.  
  274. if ( "" !== $gtm4wp_sessionid ) { 
  275. $weatherdata = get_transient( 'gtm4wp-weatherdata-'.$gtm4wp_sessionid ); 
  276.  
  277. if ( false !== $weatherdata ) { 
  278. $dataLayer[ "weatherCategory" ] = $weatherdata->weather[0]->main; 
  279. $dataLayer[ "weatherDescription" ] = $weatherdata->weather[0]->description; 
  280. $dataLayer[ "weatherTemp" ] = $weatherdata->main->temp; 
  281. $dataLayer[ "weatherPressure" ] = $weatherdata->main->pressure; 
  282. $dataLayer[ "weatherWindSpeed" ] = $weatherdata->wind->speed; 
  283. $dataLayer[ "weatherWindDeg" ] = $weatherdata->wind->deg; 
  284.  
  285. return $dataLayer;