nuxt-community / google-optimize-module

SSR friendly Google Optimize module for Nuxt.js
MIT License
215 stars 32 forks source link

Experiment is not set on Google Analytics #50

Open asokawotulo opened 3 years ago

asokawotulo commented 3 years ago

Description

I noticed that there was a discrepancy between the total number of sessions and the experiment sessions that were reported on Google Analytics.

After enabling the debug option for Google Analytics I noticed that the ga("set", "exp", exp) wasn't being called consistently.

Below is a workaround using @nuxtjs/google-analytics that seems to work for now.

// experiments.client.js
export default ({ $ga, $exp: { experimentID, $variantIndexes } }) => {
  if (!experimentID) return;
  const exp = experimentID + "." + $variantIndexes.join("-");
  $ga.set("exp", exp);
};
// nuxt.config.js
extendPlugins(plugins) {
  plugins.push("~/plugins/experiments.client.js");
  return plugins;
}

Reproduction

https://codesandbox.io/s/nuxt-google-optimize-issue-vkmho

What is Expected?

Console should output:

Executing Google Analytics commands.
Running command: ga("set", "exp", "test.0")

What is actually happening?

The command ga("set", "exp", "test.0") was never called