1
0
mirror of synced 2025-12-23 21:07:12 -05:00
Files
docs/javascripts/experiment.js
Cynthia Rich d2d56ee206 Create hello world actions quickstart (#16218)
* create hello world quickstart

* Fix image link

* Apply suggestions from code review

Co-authored-by: Rachael Sewell <rachmari@github.com>

* Add step to merge pull request before triggering workflow

* Add slash in front of file path

* Remove unused reusable

* more explaining in hello world quickstart

* Add invitation to create new repo

* Add experiment code

Co-authored-by: Rachael Sewell <rachmari@github.com>
Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com>
2020-11-10 17:05:07 +00:00

62 lines
1.6 KiB
JavaScript

import murmur from 'imurmurhash'
import { getUserEventsId, sendEvent } from './events'
const TREATMENT = 'TREATMENT'
const CONTROL = 'CONTROL'
export function bucket (test) {
const id = getUserEventsId()
const hash = murmur(test).hash(id).result()
return hash % 2 ? TREATMENT : CONTROL
}
export async function sendSuccess (test) {
return sendEvent({
type: 'experiment',
experiment_name: test,
experiment_variation: bucket(test).toLowerCase(),
experiment_success: true
})
}
const xmlns = 'http://www.w3.org/2000/svg'
export function h (tagName, attributes = {}, children = []) {
const el = ['svg', 'path'].includes(tagName)
? document.createElementNS(xmlns, tagName)
: document.createElement(tagName)
Object.entries(attributes).forEach(
([key, value]) => el.setAttribute(key, value)
)
children.forEach(child =>
typeof child === 'string'
? el.append(document.createTextNode(child))
: el.append(child)
)
return el
}
export default function () {
// const testName = '$test-name$'
// const xbucket = bucket(testName)
// if (xbucket === TREATMENT) { ... }
// x.addEventListener('click', evt => evt.preventDefault(); await sendSuccess(testName); evt())
const treatment = document.getElementById('quickstart-treatment')
if (!treatment) return
const testName = 'quickstart-hello'
const xbucket = bucket(testName)
if (xbucket === TREATMENT) {
Array.from(
document.querySelectorAll('#article-contents > *')
).forEach(el => { el.hidden = true })
treatment.hidden = false
}
document.documentElement.addEventListener('copy', () => {
sendSuccess(testName)
})
}