Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime Config Session Tracking Support #869

Merged
merged 7 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion static/js/HitchhikerJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ import RuntimeConfigReceiver from './runtime-config-receiver';
export { RuntimeConfigReceiver };

import RuntimeConfig from './runtime-config';
import AnswersExperience from './answers-experience';
const runtimeConfig = new RuntimeConfig();

import RuntimeConfigListenerRegistrant from './runtime-config-listener-registrant';
new RuntimeConfigListenerRegistrant(runtimeConfig).registerListeners();

import AnswersExperience from './answers-experience';
window.AnswersExperience = new AnswersExperience(runtimeConfig);

export * from './video-apis';
27 changes: 0 additions & 27 deletions static/js/answers-experience.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,5 @@ export default class AnswersExperience {
constructor (runtimeConfig) {
this.runtimeConfig = runtimeConfig;
this.AnswersInitializedPromise = new DeferredPromise();

runtimeConfig.registerListener({
key: 'analyticsEventsEnabled',
callback: updatedConfigOption => {
this.AnswersInitializedPromise
.then(() => this.updateAnswersAnalyticsEventsConfig(updatedConfigOption))
.catch(err => console.warn(err));
}
});
}

/**
* Update Answer's analytics events config based on new value
* of 'analyticsEventsEnabled' key in runtimeConfig
*
* @param {string|boolean} updatedConfigOption
*/
updateAnswersAnalyticsEventsConfig (updatedConfigOption) {
let option = null;
if (typeof(updatedConfigOption) === 'string') {
option = updatedConfigOption.toLowerCase() === 'true';
} else if (typeof(updatedConfigOption) === 'boolean') {
option = updatedConfigOption;
}
if (option != null) {
ANSWERS.setAnalyticsOptIn(option);
}
}
}
16 changes: 16 additions & 0 deletions static/js/runtime-config-listener-registrant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import analyticsListener from './runtime-config-listeners/analytics';
import sessionTrackingListener from './runtime-config-listeners/session-tracking';

/**
* Responsible for registering listeners to a runtime config object
*/
export default class RuntimeConfigListenerRegistrant {
cea2aj marked this conversation as resolved.
Show resolved Hide resolved
constructor(runtimeConfig) {
this._runtimeConfig = runtimeConfig;
}

registerListeners() {
this._runtimeConfig.registerListener(analyticsListener);
this._runtimeConfig.registerListener(sessionTrackingListener);
}
}
13 changes: 13 additions & 0 deletions static/js/runtime-config-listeners/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { canonicalizeBoolean } from '../utils';

export default {
cea2aj marked this conversation as resolved.
Show resolved Hide resolved
key: 'analyticsEventsEnabled',
callback: value => {
window.AnswersExperience.AnswersInitializedPromise
cea2aj marked this conversation as resolved.
Show resolved Hide resolved
.then(() => {
const canonicalizedValue = canonicalizeBoolean(value);
ANSWERS.setAnalyticsOptIn(canonicalizedValue);
})
.catch(err => console.warn(err));
cea2aj marked this conversation as resolved.
Show resolved Hide resolved
}
};
13 changes: 13 additions & 0 deletions static/js/runtime-config-listeners/session-tracking.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { canonicalizeBoolean } from '../utils';

export default {
key: 'sessionTrackingEnabled',
callback: value => {
window.AnswersExperience.AnswersInitializedPromise
.then(() => {
const canonicalizedValue = canonicalizeBoolean(value);
ANSWERS.setSessionsOptIn(canonicalizedValue);
})
.catch(err => console.warn(err));
}
}
16 changes: 16 additions & 0 deletions static/js/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
cea2aj marked this conversation as resolved.
Show resolved Hide resolved
* Returns true if the value is either the boolean value
* 'true' or a string representation of 'true'
*
* @param {string|boolean} value
* @returns {boolean}
*/
export function canonicalizeBoolean (value) {
cea2aj marked this conversation as resolved.
Show resolved Hide resolved
if (typeof(value) === 'string') {
cea2aj marked this conversation as resolved.
Show resolved Hide resolved
return value.toLowerCase() === 'true';
} else if (typeof(value) === 'boolean') {
return value;
} else {
return false;
}
}