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

Feature/cordova analytics with firebase #1542

Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions src/components/Account/Login/Login.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
enableAllTours
} from '../../App/App.actions';
import { getVoiceURI } from '../../../i18n';
import { isCordova, isElectron } from '../../../cordova-util';

export function loginSuccess(payload) {
return dispatch => {
Expand All @@ -21,6 +22,8 @@ export function loginSuccess(payload) {
payload
});
if (payload.isFirstLogin) firstLoginActions(dispatch, payload);
if (isCordova() && !isElectron())
window.FirebasePlugin.setUserId(payload.id);
};
}

Expand All @@ -30,6 +33,7 @@ function firstLoginActions(dispatch, payload) {
}

export function logout() {
if (isCordova() && !isElectron()) window.FirebasePlugin.setUserId(undefined);
return async dispatch => {
dispatch(setUnloggedUserLocation(null));
dispatch(updateUnloggedUserLocation());
Expand Down
22 changes: 20 additions & 2 deletions src/components/App/App.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
updateLoggedUserLocation,
updateUnloggedUserLocation
} from '../App/App.actions';
import { isCordova, isElectron } from '../../cordova-util';
export class AppContainer extends Component {
static propTypes = {
/**
Expand All @@ -37,7 +38,11 @@ export class AppContainer extends Component {
* App language
*/
lang: PropTypes.string.isRequired,
displaySettings: PropTypes.object.isRequired
displaySettings: PropTypes.object.isRequired,
/**
* User Id
*/
userId: PropTypes.string
};

componentDidMount() {
Expand All @@ -58,12 +63,24 @@ export class AppContainer extends Component {
}
};

const initCVAGa4 = () => {
const { isLogged, userId } = this.props;
if (!isElectron()) {
if (isLogged) {
window.FirebasePlugin.setUserId(userId);
}
window.FirebasePlugin.logEvent('page_view');
}
};

registerServiceWorker(
this.handleNewContentAvailable,
this.handleContentCached
);

localizeUser();

if (isCordova()) initCVAGa4();
}

handleNewContentAvailable = () => {
Expand Down Expand Up @@ -119,7 +136,8 @@ const mapStateToProps = state => ({
isLogged: isLogged(state),
lang: state.language.lang,
displaySettings: state.app.displaySettings,
isDownloadingLang: state.language.downloadingLang.isdownloading
isDownloadingLang: state.language.downloadingLang.isdownloading,
userId: state.app.userData.id
});

const mapDispatchToProps = {
Expand Down
17 changes: 16 additions & 1 deletion src/cordova-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,22 @@ const configFacebookPlugin = () => {

export const cvaTrackEvent = (category, action, label) => {
try {
window.ga.trackEvent(category, action, label);
const convertEventToNewNomenclature = name => {
const inLowerCase = name.toLowerCase();
const event_name = inLowerCase.replace(/\s/g, '_');
return event_name;
};
const event_name = convertEventToNewNomenclature(action);

const eventOptions = label
? {
event_category: category,
event_label: label
}
: {
event_category: category
};
if (!isElectron()) window.FirebasePlugin.logEvent(event_name, eventOptions);
} catch (err) {
console.log(err.message);
}
Expand Down