Skip to content

Commit

Permalink
added roles to ga user_property
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurBlanchon committed Mar 1, 2024
1 parent 6240da4 commit ab00d4f
Show file tree
Hide file tree
Showing 13 changed files with 273 additions and 201 deletions.
28 changes: 21 additions & 7 deletions analytics/ga.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createHash } from 'crypto'
import Guild from '../mongodb/models/guilds.js'
import discordToMongoId from '../mongodb/utils/idConversion/discordToMongoId.js';
import getUserRoles from '../discord/utils/getUserRoles.js';

export default async function ga ( guildId, clientId, user, events, debug = false ) {

Expand All @@ -11,6 +12,16 @@ export default async function ga ( guildId, clientId, user, events, debug = fals
// HASH user.id for privacy protection => SHOULD ADD: Salt at bot level !
const memberIdforGA = createHash("sha256").update( user?.id ).digest('base64');

// GET member roles
const memberRoles = await getUserRoles( guildId, user.id );
const roles = {};
for ( let i = 0; i < Math.min(memberRoles.length, 20); i++ ){
const gaRole = `role_${memberRoles[i]}`;
const cleanedGaRole = gaRole.replace(/[^a-zA-Z0-9_]/g, '');
roles[ cleanedGaRole ] = { value: true };
}
console.log('roles: ', roles)

// SET core & client analytics parameters
const measurementId = process.env.GA_MEASUREMENT_ID;
const apiSecret = process.env.GA_SECRET_KEY;
Expand All @@ -31,6 +42,7 @@ export default async function ga ( guildId, clientId, user, events, debug = fals
body: JSON.stringify({
client_id: clientId ? clientId : memberIdforGA,
user_id: memberIdforGA,
user_properties: { ...roles },
/*user_properties: {
member_status: {
value: "OG"
Expand All @@ -39,7 +51,7 @@ export default async function ga ( guildId, clientId, user, events, debug = fals
events
})
});
console.log('ga fired', queryUrl + queryParams);
console.log('ga fired for', measurementId);
} else {
console.error(`GA parameters missing from .ENV => GA_MEASUREMENT_ID: ${ !!measurementId ? '✅' : '❌' }, GA_SECRET_KEY: ${ !!apiSecret ? '✅' : '❌' }.`)
}
Expand All @@ -50,6 +62,7 @@ export default async function ga ( guildId, clientId, user, events, debug = fals
body: JSON.stringify({
client_id: clientId ? clientId : memberIdforGA,
user_id: memberIdforGA,
user_properties: { ...roles },
/*user_properties: {
member_status: {
value: "OG"
Expand All @@ -58,7 +71,7 @@ export default async function ga ( guildId, clientId, user, events, debug = fals
events
})
});
console.log('ga fired', queryUrl + queryParamsClient);
console.log('ga fired for', guildFromDb?.gaTag );
} else {
console.error(`GA parameters missing for guildId: ${guildId} => gaTag: ${ !!guildFromDb?.gaTag ? '✅' : '❌' }, gaApiKey: ${ !!guildFromDb?.gaApiKey ? '✅' : '❌' }.`)
}
Expand All @@ -69,16 +82,17 @@ export default async function ga ( guildId, clientId, user, events, debug = fals
body: JSON.stringify({
client_id: clientId ? clientId : memberIdforGA,
user_id: memberIdforGA,
user_properties: {
member_status: {
value: "OG"
user_properties: { ...roles },
/*user_properties: {
property: {
value: value
}
},
},*/
events
})
});
const jsonResponse = await response.json();
console.log('ga fired in debug', queryUrl + queryParams)
console.log('ga fired in debug for', measurementId)
return jsonResponse
}
}
2 changes: 0 additions & 2 deletions analytics/gaGuildMemberJoin.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ export default async function gaGuildMemberJoin ( member, inviteCode ){
},
});

console.log("events: ", events);

// Send the events to GA using our measurementId and apiSecret
const debug = await ga ( member.guild.id , clientId, member.user, events, false )
console.log(debug)
Expand Down
6 changes: 1 addition & 5 deletions analytics/gaMessageSent.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,9 @@ export default async function gaMessageSent ( message ){
},
});

console.log(message)
console.log("events: ", events);

// Send the events to GA using our measurementId and apiSecret
if (message.content.length) {
const debug = await ga ( guildId, null, message.author, events, false );
console.log(debug)
console.log('debug: ', debug)
}

}
3 changes: 2 additions & 1 deletion discord/commands/settings/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export const command = {
// Get the parameters
const gaTag = interaction.options.getString('tag-id') ?? null;
const apiSecret = interaction.options.getString('api-secret') ?? null;
const apiSecretMasked = apiSecret ? `${apiSecret.slice(0, 2)}...${apiSecret.slice(-3)}` : null;

/// UPDATE ONBOARDING CHANNEL IN DB
if (gaTag && apiSecret){
Expand All @@ -126,7 +127,7 @@ export const command = {
if (guildUpdate?.modifiedCount) {

interaction.editReply({
content: `✅ Google Analytics config for ${interaction.guild.name} updated. ga-tag: ${gaTag} - api-secret: ${apiSecret}`,
content: `✅ Google Analytics config for ${interaction.guild.name} updated. ga-tag: ${gaTag} - api-secret: ${apiSecretMasked}`,
embeds: [ ],
components: [ ],
}); // edit the 1st response message
Expand Down
18 changes: 11 additions & 7 deletions discord/events/guildCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import loadGuildInvites from '../scripts/loadGuildInvites.js';
export const event = {
name: Events.GuildCreate,
async execute(guild) {
// LOADING empty campaigns collection for guild
guild.client.campaigns.set(guild.id, new Collection());
// Updating the guild in DB
const guildFromDb = await saveGuild(guild);
// Loading guild's invites for matching on MemberCreate
loadGuildInvites(guild);
},
try {
// LOADING empty campaigns collection for guild
guild.client.campaigns.set(guild.id, new Collection());
// Updating the guild in DB
const guildFromDb = await saveGuild(guild);
// Loading guild's invites for matching on MemberCreate
loadGuildInvites(guild);
} catch (e) {
console.warn('Error on guildCreate event: ', e);
}
}
};
12 changes: 8 additions & 4 deletions discord/events/guildDelete.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import { Events } from 'discord.js';
export const event = {
name: Events.GuildDelete,
execute(guild) {
// We've been removed from a Guild. Let's delete all their invites
guild.client.invites.delete(guild.id);
// UNLOAD joiners collection for guild
guild.client.joiners.delete(guild.id);
try {
// We've been removed from a Guild. Let's delete all their invites
guild.client.invites.delete(guild.id);
// UNLOAD joiners collection for guild
guild.client.joiners.delete(guild.id);
} catch (e) {
console.warn('Error on guildDelete event: ', e);
}
},
};
Loading

0 comments on commit ab00d4f

Please sign in to comment.