Skip to content

Commit

Permalink
feat: share countly data with ipfs desktop (#1136)
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
  • Loading branch information
hacdias authored Sep 12, 2019
1 parent 4546a8c commit cf78641
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 26 deletions.
135 changes: 135 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@tableflip/react-dropdown": "^1.3.0",
"@tableflip/react-inspector": "^2.3.0",
"brace": "^0.11.1",
"change-case": "^3.1.0",
"chart.js": "^2.8.0",
"cids": "^0.7.1",
"countly-sdk-web": "^19.8.0",
Expand Down
64 changes: 52 additions & 12 deletions src/bundles/analytics.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import root from 'window-or-global'
import changeCase from 'change-case'
import { createSelector } from 'redux-bundler'

// Only record specific actions listed here.
const ASYNC_ACTIONS_TO_RECORD = [
Expand All @@ -10,7 +12,8 @@ const ASYNC_ACTIONS_TO_RECORD = [
'FILES_MOVE',
'FILES_DELETE',
'FILES_DOWNLOADLINK',
'EXPERIMENTS_TOGGLE'
'EXPERIMENTS_TOGGLE',
'DESKTOP_SETTING_TOGGLE'
]

const ASYNC_ACTION_RE = new RegExp(`^${ASYNC_ACTIONS_TO_RECORD.join('_|')}`)
Expand All @@ -34,6 +37,22 @@ const consentGroups = {
safe: ['sessions', 'events', 'views', 'location']
}

function addConsent (consent, store) {
root.Countly.q.push(['add_consent', consent])

if (store.selectIsIpfsDesktop()) {
store.doDesktopAddConsent(consent)
}
}

function removeConsent (consent, store) {
root.Countly.q.push(['remove_consent', consent])

if (store.selectIsIpfsDesktop()) {
store.doDesktopRemoveConsent(consent)
}
}

const createAnalyticsBundle = ({
countlyUrl = 'https://countly.ipfs.io',
countlyAppKey = pickAppKey(),
Expand Down Expand Up @@ -63,6 +82,7 @@ const createAnalyticsBundle = ({

if (store.selectIsIpfsDesktop()) {
Countly.app_version = store.selectDesktopVersion()
Countly.q.push(['change_id', store.selectDesktopCountlyDeviceId(), true])
}

// Configure what to track. Nothing is sent without user consent.
Expand All @@ -75,7 +95,7 @@ const createAnalyticsBundle = ({

if (store.selectAnalyticsEnabled()) {
const consent = store.selectAnalyticsConsent()
Countly.q.push(['add_consent', consent])
addConsent(consent, store)
}

store.subscribeToSelectors(['selectRouteInfo'], ({ routeInfo }) => {
Expand Down Expand Up @@ -109,8 +129,22 @@ const createAnalyticsBundle = ({
const durationInSeconds = (root.performance.now() - start) / 1000
let key = state === 'FAILED' ? action.type : name

if (name === 'EXPERIMENTS_TOGGLE') {
key += `_${action.payload.key}`
// Costum code for experiments toggle and desktop settings toggle.
// This way we can detect if we're enabling or disabling an option.
if (name === 'EXPERIMENTS_TOGGLE' || name === 'DESKTOP_SETTING_TOGGLE') {
key = name === 'EXPERIMENTS_TOGGLE'
? 'EXPERIMENTS_'
: 'DESKTOP_SETTING_'

key += changeCase.constantCase(action.payload.key)

if (state === 'FAILED') {
key += '_FAILED'
} else {
key += action.payload.value
? '_ENABLED'
: '_DISABLED'
}
}

root.Countly.q.push(['add_event', {
Expand Down Expand Up @@ -186,9 +220,15 @@ const createAnalyticsBundle = ({
return false
},

selectAnalyticsActionsToRecord: () => {
return Array.from(ASYNC_ACTIONS_TO_RECORD)
},
selectAnalyticsActionsToRecord: createSelector(
'selectIsIpfsDesktop',
'selectDesktopCountlyActions',
(isDesktop, desktopActions) => {
return isDesktop
? desktopActions.concat(ASYNC_ACTIONS_TO_RECORD).sort()
: Array.from(ASYNC_ACTIONS_TO_RECORD).sort()
}
),

doToggleAnalytics: () => ({ dispatch, store }) => {
const enable = !store.selectAnalyticsEnabled()
Expand All @@ -200,13 +240,13 @@ const createAnalyticsBundle = ({
},

doDisableAnalytics: () => ({ dispatch, store }) => {
root.Countly.q.push(['remove_consent', consentGroups.all])
removeConsent(consentGroups.all, store)
dispatch({ type: 'ANALYTICS_DISABLED', payload: { consent: [] } })
},

doEnableAnalytics: () => ({ dispatch, store }) => {
root.Countly.q.push(['remove_consent', consentGroups.all])
root.Countly.q.push(['add_consent', consentGroups.safe])
removeConsent(consentGroups.all, store)
addConsent(consentGroups.safe, store)
dispatch({ type: 'ANALYTICS_ENABLED', payload: { consent: consentGroups.safe } })
},

Expand All @@ -220,12 +260,12 @@ const createAnalyticsBundle = ({
},

doRemoveConsent: (name) => ({ dispatch, store }) => {
root.Countly.q.push(['remove_consent', name])
removeConsent(name, store)
dispatch({ type: 'ANALYTICS_REMOVE_CONSENT', payload: { name } })
},

doAddConsent: (name) => ({ dispatch, store }) => {
root.Countly.q.push(['add_consent', name])
addConsent(name, store)
dispatch({ type: 'ANALYTICS_ADD_CONSENT', payload: { name } })
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/bundles/analytics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ function createStore (analyticsOpts = {}) {
},
{
name: 'mockIpfsDesktopBundle',
selectIsIpfsDesktop: () => false
selectIsIpfsDesktop: () => false,
selectDesktopCountlyActions: () => ([])
},
createAnalyticsBundle(analyticsOpts)
)()
Expand Down
Loading

0 comments on commit cf78641

Please sign in to comment.