Skip to content

Commit

Permalink
added messageReaction tracking with GA
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurBlanchon committed Mar 5, 2024
1 parent ab00d4f commit a38d613
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
61 changes: 61 additions & 0 deletions analytics/gaMessageReactionAdd.js
Original file line number Diff line number Diff line change
@@ -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)
}
}
3 changes: 3 additions & 0 deletions discord/events/messageCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
30 changes: 30 additions & 0 deletions discord/events/messageReactionAdd.js
Original file line number Diff line number Diff line change
@@ -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);
}
},
};
11 changes: 10 additions & 1 deletion discord/index.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
],
});
Expand Down

0 comments on commit a38d613

Please sign in to comment.