/* eslint-disable no-unused-vars */
/**
* Segment.IO destination functions API endpoints.
*
* @see [destination-functions](https://segment.com/docs/connections/destinations/destination-functions)
*
* @module identity
* @requires FunctionSettings
* @requires SegmentIdentifyEvent
* @requires SegmentPageEvent
* @requires SegmentTrackEvent
*/
/**
* Handle track event.
*
* @see [track](https://segment.com/docs/connections/spec/track/)
*
* @param {SegmentTrackEvent} event
* @param {FunctionSettings} settings
*/
async function onTrack(event, settings) {
if (event.userId && event.userId.match('^[0-9]*$')) {
delete event.userId;
}
console.log(event);
if (event.messageId) {
delete event.messageId;
}
const hash = crypto.createHash('sha256');
if (
event.properties.first_name &&
event.properties.first_name !== '' &&
event.properties.last_name &&
event.properties.last_name !== '' &&
event.properties.email &&
event.properties.email !== ''
) {
const fn = event.properties.first_name.toLowerCase().replace(' ', '');
const ln = event.properties.last_name.toLowerCase().replace(' ', '');
const em = event.properties.email.toLowerCase().replace(' ', '');
const hashString = fn + ln + em;
hash.update(hashString);
event.userId = hash.digest('hex');
console.log(hashString);
}
// BQ auth
// Auth BQ call
// let endpoint =
// 'https://bigquery.googleapis.com/bigquery/v2/projects/enduring-hue-184016/datasets/edw/tables/product_dim/data?selectedFields=productid';
// let headers = {
// 'Callback URL': 'https://www.getpostman.com/oauth2/callback',
// 'Auth URL': 'https://accounts.google.com/o/oauth2/auth',
// 'Access Token URL': 'https://accounts.google.com/o/oauth2/token',
// 'Client ID':
// '339379353487 - 3 mk9js9gs72kvar8bjbjlajorhacbugg.apps.googleusercontent.com',
// 'Client Secret': 'gxJyWPdTcoAxFSSzHszZxtYO',
// Scope: 'https://www.googleapis.com/auth/bigquery',
// 'Content-Type': 'application/x-www-form-urlencoded'
// };
// let body = {};
// const authUrl = new URL(endpoint);
// const authResponse = await fetch(authUrl.toString(), {
// method: 'get',
// body: body,
// headers: headers
// });
// let authKey = authResponse;
// finally we will build the track call and send it to our HTTP API
const trackEndpoint = 'https://api.segment.io/v1/track';
const trackUrl = new URL(trackEndpoint);
trackUrl.searchParams.set('ts', event.timestamp);
const enrichedEvent = await fetch(trackUrl.toString(), {
method: 'post',
body: JSON.stringify(event),
headers: new Headers({
Authorization: 'Basic ' + btoa(`${settings.apiKey}:`), // in this case our apiKey will actually be the HTTP API write key
'Content-Type': 'application/json',
}),
});
return await enrichedEvent.text();
}
/**
* Handle identify event.
*
* @see [identify](https://segment.com/docs/connections/spec/identify/)
* @param {SegmentIdentifyEvent} event
* @param {FunctionSettings} settings
*/
async function onIdentify(event, settings) {
if (event.userId && event.userId.match('^[0-9]*$')) {
delete event.userId;
}
if (event.messageId) {
delete event.messageId;
}
const hash = crypto.createHash('sha256');
if (event.traits.first_name && event.traits.first_name !== '' && event.traits.last_name && event.traits.last_name !== '' && event.traits.email && event.traits.email !== '') {
const fn = event.traits.first_name.toLowerCase().replace(' ', '');
const ln = event.traits.last_name.toLowerCase().replace(' ', '');
const em = event.traits.email.toLowerCase().replace(' ', '');
const hashString = fn + ln + em;
hash.update(hashString);
event.userId = hash.digest('hex');
console.log(hashString);
}
console.log(event);
const identifyEndpoint = 'https://api.segment.io/v1/identify';
const identifyUrl = new URL(identifyEndpoint);
const headers = new Headers({
// in this case our apiKey will actually be the HTTP API write key
Authorization: 'Basic ' + btoa(`${settings.apiKey}:`),
'Content-Type': 'application/json',
});
identifyUrl.searchParams.set('ts', event.timestamp);
const identifyEvent = await fetch(identifyUrl.toString(), {
method: 'post',
body: JSON.stringify(event),
headers: headers,
});
console.log(await identifyEvent);
return await identifyEvent.text();
}
/**
* Handle page event
*
* @see [connections](https://segment.com/docs/connections/spec/page/)
*
* @param {SegmentPageEvent} event
* @param {FunctionSettings} settings
*/
async function onPage(event, settings) {
if (event.userId && event.userId.match('^[0-9]*$')) {
delete event.userId;
}
if (event.messageId) {
delete event.messageId;
}
const hash = crypto.createHash('sha256');
if (
event.properties.first_name &&
event.properties.first_name !== '' &&
event.properties.last_name &&
event.properties.last_name !== '' &&
event.properties.email &&
event.properties.email !== ''
) {
const fn = event.properties.first_name.toLowerCase().replace(' ', '');
const ln = event.properties.last_name.toLowerCase().replace(' ', '');
const em = event.properties.email.toLowerCase().replace(' ', '');
const hashString = fn + ln + em;
hash.update(hashString);
event.userId = hash.digest('hex');
console.log(hashString);
}
console.log(event);
const pageEndpoint = 'https://api.segment.io/v1/page';
const pageUrl = new URL(pageEndpoint);
const headers = new Headers({
// in this case our apiKey will actually be the HTTP API write key
Authorization: 'Basic ' + btoa(`${settings.apiKey}:`),
'Content-Type': 'application/json',
});
pageUrl.searchParams.set('ts', event.timestamp);
const pageEvent = await fetch(pageUrl.toString(), {
method: 'post',
body: JSON.stringify(event),
headers: headers,
});
return await pageEvent.text();
}