From a38d613417149dc308e0dea5eef15bc1b577d113 Mon Sep 17 00:00:00 2001 From: ArthurOnTime <112695921+ArthurOnTime@users.noreply.github.com> Date: Tue, 5 Mar 2024 18:17:21 +0100 Subject: [PATCH] added messageReaction tracking with GA --- analytics/gaMessageReactionAdd.js | 61 ++++++++++++++++++++++++++++ discord/events/messageCreate.js | 3 ++ discord/events/messageReactionAdd.js | 30 ++++++++++++++ discord/index.js | 11 ++++- 4 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 analytics/gaMessageReactionAdd.js create mode 100644 discord/events/messageReactionAdd.js diff --git a/analytics/gaMessageReactionAdd.js b/analytics/gaMessageReactionAdd.js new file mode 100644 index 0000000..83429d7 --- /dev/null +++ b/analytics/gaMessageReactionAdd.js @@ -0,0 +1,61 @@ +import ga from './ga.js' + +export default async function gaMessageReactionAdd ( messageReaction, user ){ + + if( !messageReaction?.message?.guildId || !user ){ + throw new Error(`Parameters required => message.guildId: ${ !!messageReaction?.message?.guildId ? '✅' : '❌' }, user.id: ${ !!user?.id ? '✅' : '❌' }.`) + } + + //create an event and send to ga using the ga function + const guildId = messageReaction?.message?.guildId; + + // A queue to batch our events + const events = []; + + // Let's push the page view event + events.push({ + //name: 'message_sent', + name: 'page_view', + params: { + page_title: `discord-channel-${messageReaction?.message?.channelId}`, // REPLACE WITH CHANNEL NAME IF NO BOTTLENECK + //page_location: `https://discordlinks.com/discord/channel/${message.channelId}`, + page_location: `https://discord.com/channels/${messageReaction?.message?.guildId}/${messageReaction?.message?.channelId}`, + guild_id: messageReaction?.message?.guildId, + channel_id: messageReaction?.message?.channelId, + //raw_url: `https://discord.com/channels/${guildId}/${channelId}`, + //language: "en", + //page_referrer: "apple.com", + //screen_resolution: "" + engagement_time_msec: "1" // Needed for non-zero user count + }, + }); + + + // Let's push the message_sent event + events.push({ + //name: 'message_sent', + name: 'message_reaction_add', + params: { + page_title: `discord-channel-${messageReaction?.message?.channelId}`, + page_location: `https://discord.com/channels/${messageReaction?.message?.guildId}/${messageReaction?.message?.channelId}`, + emoji_name: messageReaction?._emoji.name, + //emoji_id: messageReaction?._emoji.id, // can be NULL => event is not processed in GA if one property is NULL + //emoji_animated: messageReaction?._emoji.animated, // can be NULL => event is not processed in GA if one property is NULL + guild_id: messageReaction?.message?.guildId, + channel_id: messageReaction?.message?.channelId, + author_id: messageReaction?.message?.author?.id, + message_lenght: messageReaction?.message?.content.length, + message_url: `https://discord.com/channels/${messageReaction?.message?.guildId}/${messageReaction?.message?.channelId}/${messageReaction?.message?.id}`, + //language: "en", + //page_referrer: "apple.com", + //screen_resolution: "" + //engagement_time_msec: "1" // Needed for non-zero user count + }, + }); + + // Send the events to GA using our measurementId and apiSecret + if (messageReaction?.message?.content.length) { + const debug = await ga ( guildId, null, user, events, false ); + console.log('debug: ', debug) + } +} \ No newline at end of file diff --git a/discord/events/messageCreate.js b/discord/events/messageCreate.js index 1aed9c2..b0a75f6 100644 --- a/discord/events/messageCreate.js +++ b/discord/events/messageCreate.js @@ -9,6 +9,9 @@ export const event = { name: Events.MessageCreate, async execute(message) { try { + + if (message.partial) { await message.fetch() }; // Make sure the message is fully loaded + // Sending a message_sent event to Google Analytics gaMessageSent( message ); diff --git a/discord/events/messageReactionAdd.js b/discord/events/messageReactionAdd.js new file mode 100644 index 0000000..a4a4381 --- /dev/null +++ b/discord/events/messageReactionAdd.js @@ -0,0 +1,30 @@ +import { Events } from 'discord.js'; +import gaMessageReactionAdd from '../../analytics/gaMessageReactionAdd.js' +import Member from '../../mongodb/models/members.js'; +import discordToMongoId from '../../mongodb/utils/idConversion/discordToMongoId.js'; +import saveMessage from '../../mongodb/utils/saveMessage.js'; +import ga from '../../analytics/ga.js'; +import gaMessageSent from '../../analytics/gaMessageSent.js' + +export const event = { + name: Events.MessageReactionAdd, + async execute( messageReaction , user ) { + + try { + + // fetch the message if it's not cached + const message = !messageReaction.message.author + ? await messageReaction.message.fetch() + : messageReaction.message; + + // Sending a message_sent event to Google Analytics + gaMessageReactionAdd( messageReaction, user ); + + console.log( 'messageReaction', messageReaction ); + console.log( 'user', user) + + } catch (e) { + console.warn('Error on messageReactionAdd event: ', e); + } + }, +}; \ No newline at end of file diff --git a/discord/index.js b/discord/index.js index d2f4f7b..28df417 100644 --- a/discord/index.js +++ b/discord/index.js @@ -1,6 +1,6 @@ import fs from 'fs'; import path from 'path' -import { Client, Collection, Events, GatewayIntentBits } from 'discord.js'; +import { Client, Collection, Partials, GatewayIntentBits } from 'discord.js'; import dotenv from 'dotenv'; dotenv.config(); import { fileURLToPath } from 'url'; @@ -28,12 +28,21 @@ const __dirname = path.dirname(__filename); const token = process.env.DISCORD_BOT_SECRET; client = new Client({ + partials: [ + Partials.Message, + //Partials.Channel, + Partials.Reaction, + //Partials.User, + // Add partials here + ], intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildInvites, + GatewayIntentBits.GuildMessageReactions, + GatewayIntentBits.GuildPresences, // Add intents here ], });