Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: nuxt-community/google-optimize-module
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: lengjing/nuxt-optimize-module
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 5 commits
  • 6 files changed
  • 1 contributor

Commits on Sep 21, 2020

  1. remove google

    lengjing committed Sep 21, 2020
    Copy the full SHA
    112b17c View commit details

Commits on Sep 22, 2020

  1. chore(release): 0.5.5

    lengjing committed Sep 22, 2020
    Copy the full SHA
    c6a35cb View commit details
  2. update readme

    lengjing committed Sep 22, 2020
    Copy the full SHA
    dad8b80 View commit details

Commits on Sep 23, 2020

  1. feat: support for several experiments

    lengjing committed Sep 23, 2020
    Copy the full SHA
    a7cf143 View commit details
  2. chore(release): 0.5.6

    lengjing committed Sep 23, 2020
    Copy the full SHA
    1927341 View commit details
Showing with 84 additions and 76 deletions.
  1. +20 −1 CHANGELOG.md
  2. +13 −22 README.md
  3. +1 −2 lib/module.js
  4. +35 −38 lib/plugin.js
  5. +4 −4 package.json
  6. +11 −9 test/fixture/experiments/index.js
21 changes: 20 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
# Change Log
# Changelog

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [0.5.6](https://github.com/lengjing/nuxt-optimize-module/compare/v0.5.5...v0.5.6) (2020-09-23)


### Features

* support for several experiments ([a7cf143](https://github.com/lengjing/nuxt-optimize-module/commit/a7cf14334e6fa53eb416eaec7b5ce624886ed818))

### [0.5.5](https://github.com/lengjing/nuxt-optimize-module/compare/v0.5.4...v0.5.5) (2020-09-22)


### Features

* exclude bots to assign experiments ([#28](https://github.com/lengjing/nuxt-optimize-module/issues/28)) ([16d4a41](https://github.com/lengjing/nuxt-optimize-module/commit/16d4a41e6ba95e7b0e7962e77167a10223557a5f))


### Bug Fixes

* **deps:** update dependency cookie to ^0.4.0 ([1f0d88b](https://github.com/lengjing/nuxt-optimize-module/commit/1f0d88b7a06d33c8c89966626ea6822f0e6bb4b2))

<a name="0.5.4"></a>
## [0.5.4](https://github.com/alibaba-aero/nuxt-google-optimize/compare/v0.5.3...v0.5.4) (2019-05-25)

35 changes: 13 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# nuxt-google-optimize
# nuxt-optimize-module

[![npm (scoped with tag)](https://img.shields.io/npm/v/nuxt-google-optimize/latest.svg?style=flat-square)](https://npmjs.com/package/nuxt-google-optimize)
[![npm](https://img.shields.io/npm/dt/nuxt-google-optimize.svg?style=flat-square)](https://npmjs.com/package/nuxt-google-optimize)
[![CircleCI](https://img.shields.io/circleci/project/github/alibaba-aero/nuxt-google-optimize.svg?style=flat-square)](https://circleci.com/gh/alibaba-aero/nuxt-google-optimize)
[![Codecov](https://img.shields.io/codecov/c/github/alibaba-aero/nuxt-google-optimize.svg?style=flat-square)](https://codecov.io/gh/alibaba-aero/nuxt-google-optimize)
[![Dependencies](https://david-dm.org/alibaba-aero/nuxt-google-optimize/status.svg?style=flat-square)](https://david-dm.org/alibaba-aero/nuxt-google-optimize)
[![npm (scoped with tag)](https://img.shields.io/npm/v/nuxt-optimize-module/latest.svg?style=flat-square)](https://npmjs.com/package/nuxt-optimize-module)
[![npm](https://img.shields.io/npm/dt/nuxt-optimize-module.svg?style=flat-square)](https://npmjs.com/package/nuxt-optimize-module)
[![CircleCI](https://img.shields.io/circleci/project/github/alibaba-aero/nuxt-optimize-module.svg?style=flat-square)](https://circleci.com/gh/alibaba-aero/nuxt-optimize-module)
[![Codecov](https://img.shields.io/codecov/c/github/alibaba-aero/nuxt-optimize-module.svg?style=flat-square)](https://codecov.io/gh/alibaba-aero/nuxt-optimize-module)
[![Dependencies](https://david-dm.org/alibaba-aero/nuxt-optimize-module/status.svg?style=flat-square)](https://david-dm.org/alibaba-aero/nuxt-optimize-module)
[![js-standard-style](https://img.shields.io/badge/code_style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com)

> SSR friendly Google Optimize module for Nuxt.js
> SSR friendly Optimize module for Nuxt.js
[📖 **Release Notes**](./CHANGELOG.md)

@@ -22,31 +22,22 @@

## Setup

- Add `nuxt-google-optimize` dependency using yarn or npm to your project
- Add `nuxt-optimize-module` dependency using yarn or npm to your project
```sh
yarn add nuxt-google-optimize
yarn add nuxt-optimize-module
```
OR
```sh
npm install nuxt-google-optimize --save
npm install nuxt-optimize-module --save
```

- Add `nuxt-google-optimize` to `modules` section of `nuxt.config.js`
- Add `nuxt-optimize-module` to `modules` section of `nuxt.config.js`

```js
{
modules: [
'nuxt-google-optimize',
],

// Optional options
googleOptimize: {
// experimentsDir: '~/experiments',
// maxAge: 60 * 60 * 24 * 7 // 1 Week
// pushPlugin: true,
// excludeBots: true,
// botExpression: /(bot|spider|crawler)/i
}
'nuxt-optimize-module',
]
}
```

3 changes: 1 addition & 2 deletions lib/module.js
Original file line number Diff line number Diff line change
@@ -11,13 +11,12 @@ module.exports = async function module (moduleOptions) {
botExpression: /(bot|spider|crawler)/i
},

this.options.googleOptimize,
moduleOptions
)

const pluginOpts = {
src: resolve(__dirname, 'plugin.js'),
fileName: 'google-optimize.js',
fileName: 'optimize.js',
options
}

73 changes: 35 additions & 38 deletions lib/plugin.js
Original file line number Diff line number Diff line change
@@ -3,52 +3,49 @@ import { parse as parseCookie, serialize as serializeCookie } from 'cookie'
import experiments from '<%= options.experimentsDir %>'

export default function (ctx, inject) {
// Assign experiment and variant to user
assignExperiment(ctx)

// Google optimize integration
googleOptimize(ctx)
// apply experiment
applyExperiment(ctx)

// Inject $exp
inject('exp', ctx.experiment)
}

function applyExperiment(ctx){
ctx.experiment = experiments.map((experiment, experimentIndex) =>{
return assignExperiment(ctx, experiment, experimentIndex)
});
}

// Choose experiment and variant
function assignExperiment(ctx) {
let experimentIndex = -1
let experiment = {}
function assignExperiment(ctx, experiment, experimentIndex) {
let variantIndexes = []
let classes = []

// Try to restore from cookie
const cookie = getCookie(ctx, 'exp') || '' // experimentID.var1-var2
const cookie = getCookie(ctx, 'exp.' + experiment.experimentID) || '' // experimentID.var1-var2
const [cookieExp, cookieVars] = cookie.split('.')

if (cookieExp && cookieVars) {
// Try to find experiment with that id
experimentIndex = experiments.findIndex(exp => exp.experimentID === cookieExp)
experiment = experiments[experimentIndex]

// Variant indexes
variantIndexes = cookieVars.split('-').map(v => parseInt(v))
}

// Choose one experiment
const experimentWeights = experiments.map(exp => exp.weight === undefined ? 1 : exp.weight)
let retries = experiments.length
while (experimentIndex === -1 && retries-- > 0) {
experimentIndex = weightedRandom(experimentWeights)
experiment = experiments[experimentIndex]

// Check if current user is eligible for experiment
if (typeof experiment.isEligible === 'function') {
if (!experiment.isEligible(ctx)) {
// Try another one
experimentWeights[experimentIndex] = 0
experimentIndex = -1
}
}
}
// const experimentWeights = experiments.map(exp => exp.weight === undefined ? 1 : exp.weight)
// let retries = experiments.length
// while (experimentIndex === -1 && retries-- > 0) {
// experimentIndex = weightedRandom(experimentWeights)
// experiment = experiments[experimentIndex]

// // Check if current user is eligible for experiment
// if (typeof experiment.isEligible === 'function') {
// if (!experiment.isEligible(ctx)) {
// // Try another one
// experimentWeights[experimentIndex] = 0
// experimentIndex = -1
// }
// }
// }

if (experimentIndex !== -1 && !skipAssignment(ctx)) {
// Validate variantIndexes against experiment (coming from cookie)
@@ -65,7 +62,7 @@ function assignExperiment(ctx) {
// Write exp cookie if changed
const expCookie = experiment.experimentID + '.' + variantIndexes.join('-')
if (cookie !== expCookie) {
setCookie(ctx, 'exp', expCookie, experiment.maxAge)
setCookie(ctx, 'exp.' + experiment.experimentID, expCookie, experiment.maxAge)
}

// Compute global classes to be injected
@@ -77,7 +74,7 @@ function assignExperiment(ctx) {
classes = []
}

ctx.experiment = {
return {
$experimentIndex: experimentIndex,
$variantIndexes: variantIndexes,
$activeVariants: variantIndexes.map(index => experiment.variants[index]),
@@ -95,7 +92,7 @@ function getCookie(ctx, name) {
const cookieStr = process.client ? document.cookie : ctx.req.headers.cookie
const cookies = parseCookie(cookieStr || '') || {}

return cookies[name]
return name ? cookies[name] : cookies
}

function setCookie(ctx, name, value, maxAge = <%= options.maxAge %>) {
@@ -120,15 +117,15 @@ function setCookie(ctx, name, value, maxAge = <%= options.maxAge %>) {
}

// https://developers.google.com/optimize/devguides/experiments
function googleOptimize({ experiment }) {
if (process.server || !window.ga || !experiment || !experiment.experimentID) {
return
}
// function googleOptimize({ experiment }) {
// if (process.server || !window.ga || !experiment || !experiment.experimentID) {
// return
// }

const exp = experiment.experimentID + '.' + experiment.$variantIndexes.join('-')
// const exp = experiment.experimentID + '.' + experiment.$variantIndexes.join('-')

window.ga('set', 'exp', exp)
}
// window.ga('set', 'exp', exp)
// }

// should we skip bots?
function skipAssignment(ctx) {
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nuxt-google-optimize",
"version": "0.5.4",
"description": "SSR friendly Google Optimize module for Nuxt.js",
"name": "nuxt-optimize-module",
"version": "0.5.6",
"description": "SSR friendly Optimize module for Nuxt.js",
"license": "MIT",
"contributors": [
{
@@ -15,7 +15,7 @@
}
],
"main": "lib/module.js",
"repository": "https://github.com/alibaba-aero/nuxt-google-optimize",
"repository": "https://github.com/lengjing/nuxt-optimize-module",
"publishConfig": {
"access": "public"
},
20 changes: 11 additions & 9 deletions test/fixture/experiments/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export default [{
name: 'test1',
experimentID: 'id1',
variants: [
{ weight: 100 },
{ weight: 0 }
],
maxAge: 120
}]
export default [
{
name: 'test1',
experimentID: 'id1',
variants: [
{ weight: 100 },
{ weight: 0 }
],
maxAge: 120
}
]