To add an event, use the following JavaScript function:
function authenticateAndSendEvent(email, password, eventType, eventData, sessionId) {
// First request to sign in and get the token using fetch
const pageUrl = window.location.href;
const signInSettings = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: email,
password: password,
returnSecureToken: true
})
};
fetch('https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyDiUCrekZpeSjIvew0QvZDFoa7Uy6cdTqE', signInSettings)
.then(response => response.json())
.then(signInResponse => {
console.log(signInResponse);
// Extract the ID token from the sign-in response
const idToken = signInResponse.idToken;
// Second request using the ID token from the first response
const eventSettings = {
method: 'POST',
headers: {
'Authorization': `Bearer ${idToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
eventType: eventType,
eventData: eventData,
sessionId: sessionId,
pageUrl: pageUrl
})
};
return fetch('https://us-central1-fir-event-stream.cloudfunctions.net/addEvent', eventSettings);
})
.then(eventResponse => eventResponse.json())
.then(eventData => {
console.log(eventData);
})
.catch(error => {
console.error('Request failed:', error);
});
}
// Example usage:
const sessionId = 'session_12345';
authenticateAndSendEvent('email', 'password','purchase', 'cartid: 10142', sessionId);
You can copy the code above and use it in your JavaScript to trigger an event and log it to the Firebase backend.