本文介绍了React/Next.js 中 Google Optimize 的一般问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题可能看起来有点含糊,但那是因为我目前在我们的 React/Next.js 项目中集成 Google Optimize 时遇到了多个问题.我将尝试尽可能详细地解释我的问题和行动.但是,如果有不清楚的地方,请告诉我.

The title might seem a bit vague, but that's because I'm currently experiencing multiple problems integrating Google Optimize in our React / Next.js project. I'm going to try to explain my problems and actions as detailed as possible. However, let me know if something's unclear.

  1. 虽然在头顶添加了谷歌优化,但我们总是会遇到页面闪烁的情况.这意味着用户首先看到我们的页面,然后什么也没有(异步隐藏功能启动),然后页面再次发生变化.
  2. 我创建了一个实验,用于重新排序导航链接、删除一个导航链接并更改我们的 CTA 文本.出于测试目的,我已将变体设置为 100%.发生的情况(与问题 1 相关)是我们首先看到我们的原始网页,然后是更改,然后再次看到删除了一个导航链接的原始网页.这意味着它添加了变体,但删除了一些更改.
  3. 在动态页面上添加实验时,无法应用更改.当我运行实验时,没有任何变化,当我重新打开可视化编辑器时,它说更改存在问题.即使更改只是简单的文本更改.

我尝试过的:

  1. 添加异步隐藏"html 上的默认类名.默认情况下,这会将网页设置为隐藏.如果我使用谷歌优化同步,这很好.但是,如果我将其设置为异步,则需要 1 到 2 秒才能显示页面,这不是很好的性能.
  2. 我按照 Google 中提到的确切步骤在 Google 跟踪代码管理器中添加了 Google Optimize这里.是的,我确实将我的 async-hide 函数中的 Google Optimize 容器 ID 更改为 GTM 容器 ID.
  3. 我取消了上面的第 2 部分,并像这样手动添加了 Google Optimize.
  1. Adding the "async-hide" className by default on html. This sets the webpage by default hidden. If I use Google Optimize synchronous, this works good. If I set it to async, however, it takes 1 to 2 seconds before it shows the page, which isn't good performance.
  2. I added Google Optimize in Google Tag Manager by following the exact steps mentioned by Google here. Yes, I did change the Google Optimize container ID in my async-hide function to GTM container ID.
  3. I undid part 2 above and added Google Optimize manually like this.
<Html lang="en" className="async-hide">
<Head>
  {/*
    Google Optimize Ant-Flicker Snippet
    https://support.google.com/optimize/answer/9692472?ref_topic=6197443
  */}
  <style
    dangerouslySetInnerHTML={{
      __html: `.async-hide { opacity: 0 !important}`,
    }}
  />
  <script
    dangerouslySetInnerHTML={{
      __html: `
      (function(a,s,y,n,c,h,i,d,e){s.className+=' '+y;h.start=1*new Date;
      h.end=i=function(){s.className=s.className.replace(RegExp(' ?'+y),'')};
      (a[n]=a[n]||[]).hide=h;setTimeout(function(){i();h.end=null},c);h.timeout=c;
      })(window,document.documentElement,'async-hide','dataLayer',4000,
      {'OPT-OPTIMIZE_ID':true});
    `,
    }}
  />
  <script src="https://www.googleoptimize.com/optimize.js?id=OPT-OPTIMIZE_ID"></script>

  {/* Google Tag Manager */}
  <script async src="https://www.googletagmanager.com/gtm.js?id=GTM-TAGMANAGER_ID"></script>
  <script
    dangerouslySetInnerHTML={{
      __html: `
        (function(w,l){w[l]=w[l]||[];w[l].push({'gtm.start':
        new Date().getTime(),event:'gtm.js'});
        })(window,'dataLayer');
      `,
    }}
  ></script>
...
[rest of code]

我遇到的问题

  1. 这个问题与 Next.js 有关吗?我们在 React 应用程序中使用静态多页而不是单页这一事实
  2. 在 React/Next.js 项目中实施 Google Optimize 的最佳方法是什么:通过 Google Tag Manager 或 Google Optimize
  3. 在 React/Next.js 项目中,Google Optimize 的最佳加载方法是什么:异步还是同步?

推荐答案

这是您在这里遇到的一个很常见的问题.谷歌优化和其他通过 Javascript 在客户端更改网站内容的 A/B 测试解决方案会产生闪烁效果,因为必须先下载脚本才能更改任何内容.

It's a quite common problem you're experiencing here. Google Optimize and other A/B testing solutions that change website content through Javascript on the client create a flicker effect, as the scripts have to be downloaded before it can change anything.

回答您的问题:

  1. 当您使用 next.js 时,会出现一些特殊问题.Next.js 在服务器上使用服务器(或静态渲染阶段)以及在客户端使用水合阶段.当您在水合作用开始之前放置 Google Optimize(并加载)时,它会呈现正确的实验,但一旦水合作用开始就会被覆盖.

  1. As you're working with next.js some special problems arise. Next.js uses a server (or static render phase) on the server as well as a hydration phase at the client side. When you place Google Optimize (and it loads) before the hydration started it will render the correct experiment but will get overwritten as soon as the hydration kicks in.

如何将它与 Tagmanger 正确集成:在最外部的组件中,例如<App/> 你可以使用一个 useEffect() 钩子,它会抛出一个 tagmanager 事件来触发优化实验的插入.

How could you integrate it properly with Tagmanger: In the most outer component, e.x. <App/> you could use a useEffect() hook which throws a tagmanager event to trigger the insertion of the optimize experiement.

!请注意!通过这个你会得到闪烁效果或慢关闭您的网站,因为您必须等到所有东西都补水在慢速设备上可能需要几秒钟,然后重新渲染你的内容.

!Please be aware! through this you will get a flicker effect or slowdown your site as you have to wait until everything is hydratedwhich can take some seconds on slow devices and then will rerenderyour content.

这是 Jamstack Pages 的常见问题 - 可以通过以下方式解决将实验 api 直接集成到代码中,这肯定是更复杂.

This is a common problem with Jamstack Pages - which can be solved throughintegration the experimentation api directly in the code which for sure ismore complex.

同步加载会减慢你的网站速度,因为它会阻塞渲染,直到 JS 和内容被加载,异步加载会使网站闪烁.这是使用基于标签的 A/B 测试时的两个选项 - 您必须选择较小的邪恶.

Sync loading will slow down you're site as it blocks the rendering until the JS and content is loaded, async loading will make the site flicker. That's the two options when working with tag based A/B testing - you have to choose the lesser evil.

这篇关于React/Next.js 中 Google Optimize 的一般问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 19:43