本文介绍了是否可以使用Google Optimize在Javascript中读取ExperimentId和VariationId?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google Optimize创建了A / B测试。现在我想在Javascript中阅读当前的ExperimentId和variationId。我的目标是根据给定的变体运行不同的javascript。

I have created an A/B-test using Google Optimize. Now I would like to read the current experimentId and variationId in Javascript. My goal is to run different javascript based on the given variation.

我似乎无法在文档中找到任何相关信息。有可能吗?

I can't seem to find any info on this in the documentation. Is it possible?

推荐答案

现在还有Google Optimize javascript API,这是一个更好的选择:

Now there is also the Google Optimize javascript API available that is a better option:

一旦创建实验​​(开始之前),现在可以在Optimize UI中使用experimentId。

The experimentId is now available in the Optimize UI, as soon as the experiment is created (before start).

API是已在页面中显示,您可以像这样使用它:

The API is already available in the page and you can use it like this:

google_optimize.get('<experimentId>');

(注意:这只有在加载了Optimize容器脚本后才能工作)

(note: this will work only after the Optimize container script has been loaded)

您还可以使用以下命令注册回调以运行您想要的javascript(甚至在加载Optimize脚本之前):

You can also register a callback to run the javascript that you want at any time (even before the Optimize script has been loaded) using:

function gtag() {dataLayer.push(arguments)}

function implementExperimentA(value) {
  if (value ==  '0') {
    // Provide code for visitors in the original.
  } else if (value == '1') {
    // Provide code for visitors in first variant.
  }

gtag('event', 'optimize.callback', {
    name: '<experiment_id_A>',
    callback: implementExperimentA
 });

如果你想找到experimentId和变体,你可以为任何实验注册一个回调:

If you want to find both the experimentId and variation you can register a callback for any experiment:

function implementManyExperiments(value, name) {
  if (name == '<experiment_id_A>') {
    // Provide implementation for experiment A
    if (value ==  '0') {
      // Provide code for visitors in the original.
    } else if (value == '1') {
      // Provide code for visitors in first variant.
    ...
  } else if (name == '<experiment_id_B>') {
    // Provide implementation for experiment B
    ...
}

gtag('event', 'optimize.callback', {
    callback: implementManyExperiments
 });

更多详情

这篇关于是否可以使用Google Optimize在Javascript中读取ExperimentId和VariationId?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 09:19