Skip to content

Commit

Permalink
Merge 7448b45 into e49b17d
Browse files Browse the repository at this point in the history
  • Loading branch information
romanchyla committed May 12, 2020
2 parents e49b17d + 7448b45 commit 3b4614e
Show file tree
Hide file tree
Showing 24 changed files with 880 additions and 133 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"env": {
"browser": true,
"amd": true,
"es6": false,
"es6": true,
"commonjs": false,
"jquery": true,
"mocha": true
Expand Down
28 changes: 22 additions & 6 deletions src/config/common.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,35 @@ define([], function() {
},
});

window.GoogleAnalyticsObject = '__ga__';

require(['config/discovery.vars'], function(config) {

// rca: not sure why the ganalytics is loaded here instead of inside analytics.js
// perhaps it is because it is much/little sooner this way?

// make sure that google analytics never blocks app load
setTimeout(function() {
require(['google-analytics', 'analytics'], function() {
var qa = window[window.GoogleAnalyticsObject];
qa.l = Date.now();
qa(
require(['google-analytics', 'analytics'], function(_, analytics) {
// set ganalytics debugging
//window.ga_debug = {trace: true};
analytics(
'create',
config.googleTrackingCode || '',
config.googleTrackingOptions
);

// if we ever want to modify what experiment/variant the user
// is going to receive, it has to happen here - by modifying the
// _gaexp cookie -- but at this stage we haven't yet downloaded
// optimize AND we haven't setup any of our api calls

// example that sets the variant 2 of the experiment
//document.cookie = '_gaexp=GAX1.1.WFD4u8V3QkaI5EcZ969yeQ.18459.2;';

if (config.googleOptimizeCode) {
analytics('require', config.googleOptimizeCode);
if (!config.debugExportBBB)
console.warn('AB testing will be loaded, but bbb object is not exposed. Change debugExportBBB if needed.');
}
});
}, 0);
});
Expand Down
9 changes: 7 additions & 2 deletions src/config/discovery.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ require.config({
Orcid: 'js/modules/orcid/module',
SecondOrderController: 'js/components/second_order_controller',
HotkeysController: 'js/components/hotkeys_controller',
Experiments: 'js/components/experiments',
},
services: {
Api: 'js/services/api',
Expand Down Expand Up @@ -88,7 +89,6 @@ require.config({
SearchWidget: 'js/widgets/search_bar/search_bar_widget',
PaperSearchForm: 'js/widgets/paper_search_form/widget',
Results: 'js/widgets/results/widget',
MyAdsFreeform: 'reactify!js/react/BumblebeeWidget?MyAdsFreeform',
QueryInfo: 'js/widgets/query_info/query_info_widget',
QueryDebugInfo: 'js/widgets/api_query/widget',
ExportWidget: 'es6!js/widgets/export/widget.jsx',
Expand All @@ -99,7 +99,6 @@ require.config({
PaperNetwork: 'js/wraps/paper_network',
ConceptCloud: 'js/widgets/wordcloud/widget',
BubbleChart: 'js/widgets/bubble_chart/widget',
MyAdsDashboard: 'reactify!js/react/BumblebeeWidget?MyAdsDashboard',
AuthorAffiliationTool:
'es6!js/widgets/author_affiliation_tool/widget.jsx',

Expand Down Expand Up @@ -145,6 +144,12 @@ require.config({
LibraryActionsWidget: 'es6!js/widgets/library_actions/widget.jsx',
AllLibrariesWidget: 'js/widgets/libraries_all/widget',
LibraryListWidget: 'js/widgets/library_list/widget',

// react widgets

MyAdsFreeform: 'reactify!js/react/BumblebeeWidget?MyAdsFreeform',
MyAdsDashboard: 'reactify!js/react/BumblebeeWidget?MyAdsDashboard',
RecommenderWidget: 'reactify!js/react/BumblebeeWidget?Recommender',
},
plugins: {},
},
Expand Down
2 changes: 2 additions & 0 deletions src/config/discovery.vars.js.default
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ define([], function() {
*/
googleTrackingCode: 'UA-XXXXXXXX-X',
googleTrackingOptions: 'auto',
googleOptimizeCode: null,
googleOptimizeOptions: null,


/**
Expand Down
11 changes: 7 additions & 4 deletions src/js/apps/discovery/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ define(['config/discovery.config', 'module'], function(config, module) {
updateProgress(100);

app.onBootstrap(data);

var dynConf = app.getObject('DynamicConfig');
if (dynConf && dynConf.debugExportBBB) {
window.bbb = app;
}

pubsub.publish(pubsub.getCurrentPubSubKey(), pubsub.APP_BOOTSTRAPPED);

pubsub.publish(pubsub.getCurrentPubSubKey(), pubsub.APP_STARTING);
Expand Down Expand Up @@ -135,10 +141,7 @@ define(['config/discovery.config', 'module'], function(config, module) {
return false;
});

var dynConf = app.getObject('DynamicConfig');
if (dynConf && dynConf.debugExportBBB) {
window.bbb = app;
}


// app is loaded, send timing event

Expand Down
2 changes: 1 addition & 1 deletion src/js/apps/discovery/navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ define([
var defer = $.Deferred();
app
.getObject('MasterPageManager')
.show('LandingPage', ['SearchWidget'])
.show('LandingPage', ['SearchWidget', 'RecommenderWidget'])
.then(function() {
return app.getWidget('LandingPage').then(function(widget) {
if (data && data.origin === 'SearchWidget') {
Expand Down
47 changes: 24 additions & 23 deletions src/js/components/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,33 +70,34 @@ define(['underscore', 'jquery'], function(_, $) {
}
};

var ga = window[window.GoogleAnalyticsObject];
window[window.GoogleAnalyticsObject] = function(...args) {
try {
const $dd = $.Deferred();
ga.q = ga.q || [];
ga.q.push(args);
ga.apply(ga, [
...args,
{
hitCallback: () => {
$dd.resolve();
},
},
]);
setTimeout(() => $dd.resolve(), 1000);
return $dd.promise();
} catch (e) {
console.info('google analytics event not tracked');
var buffer = [];
var gaName = window.GoogleAnalyticsObject || 'ga';

var cleanBuffer = function() {
if (window[gaName]) {
for (var i=0; i<buffer.length; i++) {
window[gaName].apply(this, buffer[i]);
}
buffer = []
}
};
}

var Analytics = function() {
adsLogger.apply(null, _.rest(arguments, 3));
return (
window[window.GoogleAnalyticsObject] &&
window[window.GoogleAnalyticsObject].apply(this, arguments)
);

if (window[gaName]) {
if (buffer.length > 0)
cleanBuffer()
window[gaName].apply(this, arguments);
return true;
}
else {
console.log('Buffering GA signal', arguments);
buffer.push(arguments);
setTimeout(cleanBuffer, 1000);
return false;
}

};

return Analytics;
Expand Down
97 changes: 97 additions & 0 deletions src/js/components/experiments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
define([
'underscore',
'jquery',
'js/components/generic_module',
'js/mixins/dependon',
'analytics',
'js/components/pubsub_events',
], function(_, $, GenericModule, Dependon, analytics, PubsubEvents) {
var Experiments = GenericModule.extend({
initialize: function() {
// store all metadata entries here
this.isRunning = false;
},

activate: function(beehive, app) {
this.setApp(app);
this.setBeeHive(beehive);
var pubsub = this.getPubSub();

if (!window.gtag) {
window.gtag = function() {
if (_.isArray(window.dataLayer)) {
window.dataLayer.push(arguments);
}
};
window.gtag('event', 'optimize.callback', {
callback: (value, name) => {
console.log(
'Experiment with ID: ' + name + ' is on variant: ' + value
);
},
});
}

pubsub.subscribe(
pubsub.APP_BOOTSTRAPPED,
_.bind(this.onAppStarted, this)
);
if (false) {
pubsub.subscribe(pubsub.NAVIGATE, function(id) {
if (id === 'SearchWidget') {
window.bbb.getWidget('RecommenderWidget').then(function(w) {
window.r = w;
var { tab } = w.getState();
// activate recommendations
if (!tab !== 1) {
w.dispatch({ type: 'SET_TAB', payload: 1 });
}
});
}
});
}
},

/**
*
* callback that can be used by external components; they can listen to BBB and then run their experiment
*
* */
subscribe: function(event, callback) {
var pubsub = this.getPubSub();
if (PubsubEvents[event]) {
pubsub.subscribe(PubsubEvents[event], callback);
}
},

subscribeOnce: function(event, callback) {
var pubsub = this.getPubSub();
if (PubsubEvents[event]) {
pubsub.subscribeOnce(PubsubEvents[event], callback);
}
},

onAppStarted: function() {
this.toggleOptimize();
},

toggleOptimize: function() {
if (!window.dataLayer) {
console.warn(
'Optimize is not available, we are not running any experiment'
);
return;
}

if (this.isRunning) {
window.dataLayer.push({ event: 'optimize.deactivate' });
} else {
window.dataLayer.push({ event: 'optimize.activate' });
}
this.isRunning = !this.isRunning;
},
});
_.extend(Experiments.prototype, Dependon.BeeHive, Dependon.App);

return Experiments;
});
9 changes: 7 additions & 2 deletions src/js/plugins/reactify.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
define([], function() {
const reactify = {
load: function(name, req, onload) {
load: (name, req, onload, config) => {
// for a build, only register
if (config.isBuild) {
return onload();
}

const parts = name.split('?');
const module = parts[0];
const component = parts[1];
req(
return req(
[module, `js/react/${component}/index`],
(loadedModule, Component) => {
// inject the react component as the view
Expand Down
62 changes: 62 additions & 0 deletions src/js/react/Recommender/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
define([], function() {
const actions = {
GET_RECOMMENDATIONS: 'GET_RECOMMENDATIONS',
GET_DOCS: 'GET_DOCS',
SET_DOCS: 'SET_DOCS',
SET_QUERY: 'SET_QUERY',
UPDATE_SEARCH_BAR: 'UPDATE_SEARCH_BAR',
GET_FULL_LIST: 'GET_FULL_LIST',
EMIT_ANALYTICS: 'EMIT_ANALYTICS',
SET_TAB: 'SET_TAB',
SET_ORACLE_TARGET: 'SET_ORACLE_TARGET',
SET_QUERY_PARAMS: 'SET_QUERY_PARAMS',
};

const actionCreators = {
getRecommendations: () => ({
type: actions.GET_RECOMMENDATIONS,
}),
getDocs: (query) => ({
type: 'API_REQUEST',
scope: actions.GET_DOCS,
options: {
type: 'GET',
target: 'search/query',
query,
},
}),
setDocs: (docs) => ({
type: actions.SET_DOCS,
payload: docs,
}),
setQuery: (query) => ({
type: actions.SET_QUERY,
payload: query,
}),
setQueryParams: (payload) => ({
type: actions.SET_QUERY_PARAMS,
payload,
}),
updateSearchBar: (text) => ({
type: actions.UPDATE_SEARCH_BAR,
payload: text,
}),
getFullList: () => ({
type: actions.GET_FULL_LIST,
}),
emitAnalytics: (payload) => ({
type: actions.EMIT_ANALYTICS,
payload,
}),
setTab: (tab) => ({
type: actions.SET_TAB,
payload: tab,
}),
setOracleTarget: (target) => ({
type: actions.SET_ORACLE_TARGET,
payload: target,
}),
};

return { ...actions, ...actionCreators };
});
Loading

0 comments on commit 3b4614e

Please sign in to comment.