Under the Development environment, create the feature flags listed in the next section. You can continue
+ the steps after you are done creating all of them. See image 1.
+
Go to Settings (under your Development environment), Keys, and copy your Client-side Environment Key.
+
+
In the file main.js under the javascript-vanilla-v2 folder, replace the existing environmentID (line 6)
+ key with yours and save.
+
Open the index.html under the javascript-vanilla-v2 folder in your browser. Now the example is already
+ working with your project!
+
Enable each of the feature flags in your development environment and refresh the app in your browser.
+ Notice what changed in your app for each feature flag you enabled.
+
We will now play with some mock identities (there is no real authentication) in our app.
+
+
Login, type john@mycompany.com, Proceed, Logout.
+
Login, type mary@mycompany.com, Proceed, Logout.
+
Login, type joseph@mycompany.com, Proceed, Logout.
+
+
+
Check the Identities (under your Development environment). You should see the Identities created there.
+
+
Now click on the john@mycompany.com identity. Click on the announcement feature. Make sure it is enabled
+ and change the value to “Welcome, John! Identity override is working!”
+
Let’s now create a Segment (right under the Project Settings).
+
+
ID: mycompany_employees
+
Description: This is the segment for all my company employees so we can test in
+ production
+ later.
+
Trait: email
+
Criteria: Matches regex
+
Value: .*@mycompany\.com
+
+
+
Go back to the Features (under Development environment), click on the announcement feature Segment
+ Overrides tab, select the mycompany_employees, enable it, and add the value: Welcome, employee! Segment
+ override is working! See image 2 and 3. You just created a Segment Override for your feature!
+
Login as john@mycompany.com, logout, login as mary@mycompany.com, logout. Notice the difference in the
+ announcement banner.
+
+
+
+ Feature Flags
+
+
+
+
+
Name
+
Enabled
+
Value
+
Description
+
+
+
+
+
new_button
+
disabled
+
empty
+
This is the “Button 1” in my app
+
+
+
coloured_button
+
disabled
+
#04A596
+
This is “Button 2” in my app and it will have the background color of whatever is the value in
+ this feature
+
+
+
font_size
+
disabled
+
18
+
Font size for the Instructions section
+
+
+
show_footer_icons
+
disabled
+
empty
+
This flag determines if we will show the footer with the links to our website, documentation,
+ and GitHub
+
+
+
announcement
+
disabled
+
This is a general announcement for all users
+
Toggle this to show or hide the announcement on the top of the page
+
+
+
json_textbox
+
disabled
+
{"indent": 4}
+
Inspect all the feature flags in a textbox in JSON format. The value is itself a JSON from which
+ we can extract values. Changing the number in the indent will change the format of the JSON.
+
+
+
+
\ No newline at end of file
diff --git a/javascript-vanilla-v2/main.js b/javascript-vanilla-v2/main.js
new file mode 100644
index 0000000..7200178
--- /dev/null
+++ b/javascript-vanilla-v2/main.js
@@ -0,0 +1,159 @@
+// https://docs.flagsmith.com/clients/javascript#initialisation-options
+// code and comments by @gnumoreno and @MatheusLasserre (github ids)
+const debug = 0
+flagsmith.init({
+ // Dev environment key: iefBdPk5jLjp3WfFjiB4MS
+ // Prod environment key: B7PbVXEWTCGhTD3njUxoL3
+ environmentID: 'iefBdPk5jLjp3WfFjiB4MS', // Required, the environment ID for your environment
+
+ api: 'https://edge.api.flagsmith.com/api/v1/', // Defaults to Flagsmith edge API
+ enableAnalytics: true, // See https://docs.flagsmith.com/flag-analytics/ for more info
+
+ // Using caching options you're reducing the number of api calls for each user.
+ // For example, if the ttl for your cache is 600000ms, basically, your cache is going to be refreshed only after 10 minutes.
+ cacheFlags: true, // stores flags in localStorage cache
+ cacheOptions: {
+ ttl: 10, // Time to Live in ms -- Default: 0. Try changing it to 1 min (60000 ms) and see what happens.
+ skipAPI: true, // If cache is available, get data from cache. Default: false.
+ },
+ // If you are using the Free plan, StartUp plan or Self-Hosting you don't have real time available.
+ // This feature is only available for Scale Up and Enterprise plan. See https://docs.flagsmith.com/advanced-use/real-time-flags
+ // realTime: true,
+ // Set as a identity if you want to fetch flags only for this specific identity.
+ // identity: 'some_identity',
+ // Set as a trait if you want to fetch flags only for this specific trait.
+ // traits: { age: 21, country: 'England', employee: true },
+
+ onChange: (oldFlags, params) => {
+ document.getElementById('loading').classList.add('hidden')
+ document.getElementById('content').classList.remove('hidden')
+ // Occurs whenever flags are changed
+ const { isFromServer } = params //determines if the update came from the server or localStorage cache
+ const jsonTextBox = flagsmith.hasFeature('json_textbox')
+ const flagsJson = flagsmith.getAllFlags()
+ const indentValue = flagsmith.getValue('json_textbox', {
+ json: true,
+ })
+ const button = flagsmith.hasFeature('new_button')
+ const colouredButton = flagsmith.hasFeature('coloured_button')
+ const colouredButtonValue = flagsmith.getValue('coloured_button')
+ const fontSize = flagsmith.hasFeature('font_size')
+ const fontSizeValue = flagsmith.getValue('font_size')
+ const showIcons = flagsmith.hasFeature('show_footer_icons')
+ const announcement = flagsmith.hasFeature('announcement')
+ const announcementValue = flagsmith.getValue('announcement')
+ const images = flagsmith.hasFeature('images')
+ // Check if the update is from server, otherwise, its the default flags.
+ // If default flags is not defined, all values will be undefined.
+ // All getters should be used outside the server check, because they need to be called BEFORE the server can send the update.
+ if (isFromServer) {
+
+ const jsonTextBoxEl = document.getElementById('json-text-box')
+ if (jsonTextBox) {
+ jsonTextBoxEl.innerHTML = JSON.stringify(
+ flagsJson,
+ '',
+ indentValue.indent,
+ )
+ jsonTextBoxEl.classList.remove('hidden')
+ } else {
+ jsonTextBoxEl.classList.add('hidden')
+ }
+
+ const buttonEl = document.getElementById('button')
+ if (button) {
+ buttonEl.classList.remove('hidden')
+ } else {
+ buttonEl.classList.add('hidden')
+ }
+
+ const colouredButtonEl = document.getElementById('coloured-button')
+ if (colouredButton) {
+ colouredButtonEl.classList.remove('hidden')
+ colouredButtonEl.style.backgroundColor = colouredButtonValue
+ } else {
+ colouredButtonEl.classList.add('hidden')
+ }
+
+ const featuresTableEl = document.getElementById('features')
+ const listEl = document.getElementById('list')
+ if (fontSize) {
+ featuresTableEl.style.fontSize = fontSizeValue + 'px'
+ listEl.style.fontSize = fontSizeValue + 'px'
+ } else {
+ featuresTableEl.style.fontSize = '16px'
+ listEl.style.fontSize = '16px'
+ }
+
+ const showIconsEl = document.getElementById('icons')
+ if (showIcons) {
+ showIconsEl.classList.remove('hidden')
+ } else {
+ showIconsEl.classList.add('hidden')
+ }
+
+ const announcementEl = document.getElementById('announcement')
+ if (announcement) {
+ announcementEl.classList.remove('hidden')
+ announcementEl.innerHTML = announcementValue
+ } else {
+ announcementEl.classList.add('hidden')
+ }
+
+ const imagesEl = document.getElementById('images')
+ if (images) {
+ imagesEl.classList.remove('hidden')
+ } else {
+ imagesEl.classList.add('hidden')
+ }
+
+ const loginButton = document.getElementById('js-login')
+ const logoutButton = document.getElementById('js-logout')
+
+ if (flagsmith.identity) {
+ loginButton.classList.add('hidden')
+ logoutButton.classList.remove('hidden')
+ } else {
+ loginButton.classList.remove('hidden')
+ logoutButton.classList.add('hidden')
+ }
+ }
+ // To enable debug mode, set debug to 1 on line 3.
+ if (debug) {
+ console.log('is_Server', isFromServer)
+ console.log('allFlags', flagsJson)
+ console.log('json_textbox', jsonTextBox, indentValue)
+ console.log('font_size', fontSize, fontSizeValue)
+ console.log('announcement', announcement, announcementValue)
+ console.log('new_button', button)
+ console.log('coloured_button', colouredButton, colouredButtonValue)
+ }
+ },
+})
+
+
+
+function login(formData) {
+ formData.preventDefault()
+ const email = formData.target.email.value
+ flagsmith.identify(email)
+ flagsmith.setTrait('email', email)
+ hideModal()
+}
+document.getElementById('login-form').addEventListener('submit', login)
+
+function showModal() {
+ document.getElementById('modal').classList.remove('hidden')
+}
+document.getElementById('js-login').addEventListener('click', showModal)
+
+function hideModal() {
+ document.getElementById('modal').classList.add('hidden')
+}
+document.getElementById('cancel-login').addEventListener('click', hideModal)
+
+function logout() {
+ flagsmith.logout()
+ hideModal()
+}
+document.getElementById('js-logout').addEventListener('click', logout)