Skip to content

Commit

Permalink
changes in app_instance_id for Google Analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
yadvirkaur committed Oct 18, 2024
1 parent ef3792f commit 8ac0d79
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 48 deletions.
19 changes: 19 additions & 0 deletions firebase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { initializeApp } = require('firebase/app');
const { getAnalytics } = require('firebase/analytics');

// Firebase configuration
const firebaseConfig = {
apiKey: 'AIzaSyCuRBniopYZcyYPbZSbkQnTJi62CfRSz0M',
authDomain: 'sttm-analytics.firebaseapp.com',
projectId: 'sttm-analytics',
storageBucket: 'sttm-analytics.appspot.com',
messagingSenderId: '693307351279',
appId: '1:693307351279:web:1837dbcd07b3ce7da63b1a',
measurementId: 'G-Z86MCV3BLS',
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);

module.exports = { analytics };
102 changes: 54 additions & 48 deletions www/main/settings/components/Setting.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable no-console */
/* eslint-disable camelcase */
import React from 'react';
import PropTypes from 'prop-types';
import { useStoreState, useStoreActions } from 'easy-peasy';
const { v4: uuidv4 } = require('uuid');

import { Switch, Checkbox } from '../../common/sttm-ui';
import { convertToCamelCase } from '../../common/utils';
Expand All @@ -10,7 +11,10 @@ import { settings } from '../../../configs/user-settings.json';
const remote = require('@electron/remote');

const { i18n } = remote.require('./app');
const analytics = remote.getGlobal('analytics');
// const analytics = remote.getGlobal('analytics');

const { getInstallations, getId } = require('firebase/installations');
const analytics = require('../../../../firebase');

const Setting = ({ settingObj, stateVar, stateFunction }) => {
const { title, type, min, max, step, options } = settingObj;
Expand All @@ -28,21 +32,21 @@ const Setting = ({ settingObj, stateVar, stateFunction }) => {
userSettingsActions[`set${convertToCamelCase(disableSetting, true)}`](false);
}
}
analytics.trackEvent({
category: 'setting',
action: userSettingsActions[stateFunction],
label: value,
});
// analytics.trackEvent({
// category: 'setting',
// action: userSettingsActions[stateFunction],
// label: value,
// });
};

const handleCheckboxChange = (event) => {
const value = event.target.checked;
userSettingsActions[stateFunction](value);
analytics.trackEvent({
category: 'setting',
action: userSettingsActions[stateFunction],
label: value,
});
// analytics.trackEvent({
// category: 'setting',
// action: userSettingsActions[stateFunction],
// label: value,
// });
};

let settingDOM;
Expand All @@ -60,48 +64,50 @@ const Setting = ({ settingObj, stateVar, stateFunction }) => {
return '';
};

const handleResetFontSizes = () => {
const { resetSettings } = settingObj;
if (resetSettings) {
resetSettings.forEach((settingKey) => {
const value = settings[settingKey].initialValue;
if (userSettings[convertToCamelCase(settingKey)] !== value) {
userSettingsActions[`set${convertToCamelCase(settingKey, true)}`](value);
}
});
}
const handleResetFontSizes = async () => {
try {
const { resetSettings } = settingObj;
if (resetSettings) {
resetSettings.forEach((settingKey) => {
const value = settings[settingKey].initialValue;
if (userSettings[convertToCamelCase(settingKey)] !== value) {
userSettingsActions[`set${convertToCamelCase(settingKey, true)}`](value);
}
});
}

const firebase_app_id = `1:693307351279:web:1837dbcd07b3ce7da63b1a`;
const api_secret = `GIupBo9tQwW5z02u8zFXDg`;
let app_instance_id = uuidv4();

fetch(`https://www.google-analytics.com/mp/collect?firebase_app_id=${firebase_app_id}&api_secret=${api_secret}`, {
method: "POST",
body: JSON.stringify({
app_instance_id: app_instance_id,
events: [{
name: 'tutorial_begin',
params: {
"campaign": "Analytics_test",
"source": "sttm",

},
}]
})
})
.then(response => {
const firebase_app_id = `1:693307351279:web:1837dbcd07b3ce7da63b1a`;
const api_secret = `GIupBo9tQwW5z02u8zFXDg`;
const installations = getInstallations();
const app_instance_id = await getId(installations);
console.log(app_instance_id);

await fetch(
`https://www.google-analytics.com/mp/collect?firebase_app_id=${firebase_app_id}&api_secret=${api_secret}`,
{
method: 'POST',
body: JSON.stringify({
app_instance_id: app_instance_id,
events: [
{
name: 'tutorial_begin',
params: {
campaign: 'Analytics_test',
source: 'sttm',
},
},
],
}),
},
).then((response) => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
console.log('Event sent successfully');
})
.catch(error => {
console.error('Error sending event:', error, error.response || error.message);
});

console.log('Button click event logged to Firebase');


} catch (error) {
console.error('Error in handleResetFontSizes:', error);
}
};

switch (type) {
Expand Down

0 comments on commit 8ac0d79

Please sign in to comment.