Skip to content

Commit 0075115

Browse files
Integrate Optimizely SDK
1 parent 8cba4f8 commit 0075115

File tree

7 files changed

+241
-6
lines changed

7 files changed

+241
-6
lines changed

package-lock.json

Lines changed: 124 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"format": "prettier --write src/"
1616
},
1717
"dependencies": {
18+
"@optimizely/optimizely-sdk": "^4.9.4",
1819
"pinia": "^2.1.3",
1920
"vue": "^3.3.4",
2021
"vue-router": "^4.2.2"

src/App.vue

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
<script setup lang="ts">
2+
import { provide } from 'vue'
23
import { RouterLink, RouterView } from 'vue-router'
34
import HelloWorld from './components/HelloWorld.vue'
5+
import optimizelySdk from '@optimizely/optimizely-sdk'
6+
7+
const optimizelyClient = optimizelySdk.createInstance({ sdkKey: 'K4UmaV5Pk7cEh2hbcjgwe' })
8+
if (optimizelyClient === null) {
9+
throw new Error('Optimizely instance is null')
10+
}
11+
12+
optimizelyClient.onReady().then(() => {
13+
console.info('optimizelyClient isValidInstance(): ', optimizelyClient.isValidInstance())
14+
15+
if (!optimizelyClient.isValidInstance()) {
16+
console.log('Optimizely client invalid.')
17+
return;
18+
}
19+
})
20+
21+
provide("optimizeClient", optimizelyClient)
422
</script>
523

624
<template>

src/components/DecisionOutput.vue

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script setup lang="ts">
2+
import { type Result } from '../types/Result'
3+
4+
const props = defineProps({
5+
result: Result
6+
})
7+
</script>
8+
9+
<template>
10+
<pre>
11+
User
12+
{{ result.userId }} saw variation
13+
{{ result.variation }} and got products sorted by
14+
{{ result.sortMethod }}
15+
</pre>
16+
</template>
17+
18+
<style scoped>
19+
pre {
20+
margin: 0px;
21+
}
22+
</style>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<template>
2+
<div class="warning" role="warning">
3+
<h2>Warning</h2>
4+
<slot> </slot>
5+
</div>
6+
</template>
7+
8+
<style scoped>
9+
.warning {
10+
color: orange;
11+
font-weight: bold;
12+
}
13+
</style>

src/types/Result.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface Result {
2+
userId: string;
3+
variation: string | null;
4+
sortMethod: unknown;
5+
}

src/views/RunView.vue

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,65 @@
1+
<script setup lang="ts">
2+
import { inject, ref } from 'vue'
3+
import { type Client, type OptimizelyUserContext } from '@optimizely/optimizely-sdk'
4+
import DecisionOutput from '../components/DecisionOutput.vue'
5+
import Warning from '../components/WarningComponent.vue'
6+
import { type Result } from '../types/Result'
7+
8+
const optimizelyClient = inject('optimizeClient') as Client
9+
10+
let projectId: string = ''
11+
const datafile = optimizelyClient.getOptimizelyConfig()?.getDatafile();
12+
if (datafile) {
13+
projectId = JSON.parse(datafile).projectId
14+
}
15+
16+
const results: Result[] = []
17+
for (let i = 0; i < 10; i++) {
18+
const userId = (Math.floor(Math.random() * (10000 - 1000) + 1000)).toString();
19+
20+
const user = optimizelyClient.createUserContext(userId)
21+
22+
const decision = user?.decide('product_sort')
23+
if (!decision) {
24+
continue
25+
}
26+
27+
results.push({
28+
userId,
29+
variation: decision.variationKey,
30+
sortMethod: decision.variables['sort_method'],
31+
})
32+
}
33+
</script>
34+
135
<template>
2-
<div class="run">
36+
<section class="run">
337
<h1>Experiment Output</h1>
4-
<p>[Loop through the results]</p>
5-
</div>
38+
<output v-if="results.length">
39+
<DecisionOutput v-for="result in results" :key="result.userId" :result="result" />
40+
</output>
41+
<Warning v-else>
42+
<p>
43+
No results found.
44+
</p>
45+
<p>Some reasons could include:</p>
46+
<ol>
47+
<li>Flag was off for everyone.</li>
48+
<li>Your sample size of visitors was too small. Rerun, or increase the iterations in the FOR loop.</li>
49+
<li>By default you have 2 keys for 2 project environments (dev/prod). Verify in Settings > Environments that you
50+
used the right key for the environment where your flag is toggled to ON.</li>
51+
</ol>
52+
<p>
53+
Check your key and project configuration on
54+
<a v-if="projectId !== ''"
55+
:href="'https://app.optimizely.com/v2/projects/' + projectId + '/settings/implementation'">settings page</a>
56+
<a v-else href="https://app.optimizely.com/v2/projects/">settings page</a>
57+
</p>
58+
</Warning>
59+
</section>
660
</template>
761

8-
<style>
62+
<style scoped>
963
@media (min-width: 1024px) {
1064
.run {
1165
min-height: 100vh;

0 commit comments

Comments
 (0)