简体   繁体   English

包括 Google Analytics wordpress

[英]Including Google Analytics wordpress

I know there are many different ways you can include the Google Analytics script into a Wordpress website, but I won't use plugins for this.我知道有很多不同的方法可以将 Google Analytics 脚本包含到 Wordpress 网站中,但我不会为此使用插件。

So the way I would like to prefer is to code it into the theme's functions file, giving me two options left.所以我更喜欢的方式是将它编码到主题的函数文件中,剩下两个选项。 (yes it's a child theme) (是的,这是一个儿童主题)

  • creating a function with analytics script inside, linking this to header/footer创建一个带有分析脚本的函数,将其链接到页眉/页脚
  • using the default way Wordpress asks to enqueue scripts at wp_enqueue_scripts使用 Wordpress 要求将脚本排入wp_enqueue_scripts的默认方式

The outcome of the above ways will be different so with my little Wordpress experience, I was wondering what should be the safest and most secure way to embed it into a Wordpress site?上述方式的结果会有所不同,所以根据我的 Wordpress 经验,我想知道将它嵌入到 Wordpress 站点的最安全和最安全的方法是什么? Eventually I might consider also the difference in loading time, also the fastest way?最终我可能还会考虑加载时间的差异,也是最快的方式?

wp_enqueue_script with parameter $in_footer set to false , is the same as hardcoding the script into the html head (in fact, it would be included in the place where wp_head() is called, which should be inside the <head></head> tags). wp_enqueue_script参数$in_footer设置为false ,与将脚本硬编码到 html head 相同(实际上,它会包含在调用wp_head()的地方,应该在<head></head>标签)。

wp_enqueue_script gives you the ability to add a dependency to the script you are adding, but for google analytics you don't need any, so from safety and security you are covered. wp_enqueue_script使您能够向要添加的脚本添加依赖项,但对于谷歌分析,您不需要任何依赖项,因此从安全和保障方面考虑。 From speed's point of view, I guess hardcoding it would be very slightly faster, but your header will look bigger, so it's up to you to trade readability and enqueue it or just add it as a script.从速度的角度来看,我想硬编码它会稍微快一点,但是您的标题会看起来更大,因此您可以交易可读性并将其加入队列或将其添加为脚本。 The header usually is not a large file anyway, so most of the times I just copy-paste the code inside the head as it is.无论如何,标头通常不是一个大文件,因此大多数时候我只是将代码按原样复制粘贴到标头中。

This code adds Analytics and excludes the traffic of connected users (useful to avoid registering your visits).此代码添加 Analytics 并排除连接用户的流量(有助于避免注册您的访问)。

<?php
   if ( !is_user_logged_in() ) {
      function addAnalytics() {
         $analyticsTag = "<!-- Google Analytics -->
                          <script> Codice di traccimento </script>
                          <!-- Google Analytics -->";
         echo $analyticsTag;
      }
      add_action( 'wp_enqueue_scripts', 'addAnalytics');
   }
?>

If you have already created a child theme just copy over the header file and add your google analytics code before the closing tag.如果您已经创建了子主题,只需复制头文件并在结束标记之前添加您的谷歌分析代码。 That is an easy option and that is what I always do for my WP sites.这是一个简单的选择,这就是我一直为我的 WP 网站所做的事情。

Google Analytics tracking code is asynchronous. Google Analytics跟踪代码是异步的。 It doesn't need to wait for the code to finish loading to continue rendering elements that come after it on the page.它不需要等待代码完成加载以继续在页面上呈现它之后的元素。 It's a good idea to get into the habit of placing the GTM container snippet in the head section because that's where asynchronously loading libraries should be placed.养成将GTM container snippet放在 head 部分的习惯是个好主意,因为这应该是异步加载库的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM