From 2e4ecfdca68f1bbea65206e76368559d2ce9c5c2 Mon Sep 17 00:00:00 2001 From: Rohit Gohri Date: Sun, 3 Mar 2019 17:09:26 +0530 Subject: [PATCH] Add basic analytics through universal-analytics #4 --- config.json | 3 ++- package.json | 4 +++- src/lib/telegram.ts | 31 ++++++++++++++++++++++++++++++- yarn.lock | 20 +++++++++++++++++--- 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/config.json b/config.json index f2b89d5..248975c 100644 --- a/config.json +++ b/config.json @@ -4,5 +4,6 @@ }, "telegram": { "token": "xxxx" - } + }, + "trackingId": "UA-xxxxxxxxx-x" } \ No newline at end of file diff --git a/package.json b/package.json index 4fefa97..1ad766e 100644 --- a/package.json +++ b/package.json @@ -29,11 +29,13 @@ "@smpx/oak": "^1.2.7", "lodash": "^4.17.11", "sm-utils": "^2.17.5", - "telegraf": "^3.26.0" + "telegraf": "^3.26.0", + "universal-analytics": "^0.4.20" }, "devDependencies": { "@types/lodash": "^4.14.120", "@types/node": "^10.12.18", + "@types/universal-analytics": "^0.4.2", "@typescript-eslint/eslint-plugin": "^1.1.0", "@typescript-eslint/parser": "^1.1.0", "eslint": "^5.12.1", diff --git a/src/lib/telegram.ts b/src/lib/telegram.ts index 9294eaf..914fccf 100644 --- a/src/lib/telegram.ts +++ b/src/lib/telegram.ts @@ -1,6 +1,6 @@ import Telegraf from 'telegraf'; import { InlineQueryResultVenue } from 'telegraf/typings/telegram-types'; -import d from 'sm-utils/d'; +import ua from 'universal-analytics'; import { getLocation, @@ -93,6 +93,13 @@ bot.on('inline_query', async (ctx) => { }; }); + const visitor = ua(cfg('trackingId'), {cid: String(ctx.update.inline_query.from.id), strictCidFormat: false}) + visitor.event({ + ec: 'inline query', + ea: 'search', + el: ctx.update.inline_query.query + }).send(); + return ctx.answerInlineQuery(result.slice(0, 50), { next_offset: result.length > 50 ? String(offset + 50) : '', // cache_time: 0, @@ -100,7 +107,29 @@ bot.on('inline_query', async (ctx) => { }); }); +bot.on('chosen_inline_result', async (ctx) => { + if (!ctx.update.chosen_inline_result) return; + + const visitor = ua(cfg('trackingId'), {cid: String(ctx.update.chosen_inline_result.from.id), strictCidFormat: false}) + visitor.event({ + ec: 'inline query', + ea: 'selected', + el: ctx.update.chosen_inline_result.query, + ev: ctx.update.chosen_inline_result.result_id + }).send(); +}); + bot.on('message', async (ctx) => { + if (!ctx.message || !ctx.message.from) return; + + const visitor = ua(cfg('trackingId'), {cid: String(ctx.message.from.id), strictCidFormat: false}) + + visitor.event({ + ec: 'message', + ea: 'received', + el: ctx.message.text, + }).send(); + return ctx.reply(`This is an inline bot, please invoke it by starting your message with: @${bot.options.username}`); }) diff --git a/yarn.lock b/yarn.lock index f4fcbe5..c0533f4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -217,6 +217,11 @@ resolved "https://registry.yarnpkg.com/@types/tough-cookie/-/tough-cookie-2.3.5.tgz#9da44ed75571999b65c37b60c9b2b88db54c585d" integrity sha512-SCcK7mvGi3+ZNz833RRjFIxrn4gI1PPR3NtuIS+6vMkvmsGjosqTJwRt5bAEFLRz+wtJMWv8+uOnZf2hi2QXTg== +"@types/universal-analytics@^0.4.2": + version "0.4.2" + resolved "https://registry.yarnpkg.com/@types/universal-analytics/-/universal-analytics-0.4.2.tgz#d21a122a984bf8261eb206bcb7ccb1c0e8353d04" + integrity sha512-ndv0aYA5tNPdl4KYUperlswuiSj49+o7Pwx8hrCiE9x2oeiMrn7A2jXFDdTLFIymiYZImDX02ycq0i6uQ3TL0A== + "@typescript-eslint/eslint-plugin@^1.1.0": version "1.1.0" resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-1.1.0.tgz#80f44f44f400eb7dc56040190d369443ff7fffc8" @@ -1007,7 +1012,7 @@ debug@2.6.9, debug@^2.1.2, debug@^2.2.0, debug@^2.3.3: dependencies: ms "2.0.0" -debug@^3.1.0: +debug@^3.0.0, debug@^3.1.0: version "3.2.6" resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.6.tgz#e83d17de16d8a7efb7717edbe5fb10135eee629b" integrity sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ== @@ -4041,7 +4046,7 @@ request-promise-native@^1.0.5: stealthy-require "^1.1.0" tough-cookie ">=2.3.3" -request@^2.87.0: +request@^2.87.0, request@^2.88.0: version "2.88.0" resolved "https://registry.yarnpkg.com/request/-/request-2.88.0.tgz#9c2fca4f7d35b592efe57c7f0a55e81052124fef" integrity sha512-NAqBSrijGLZdM0WZNsInLJpkJokL72XYjUpnB0iwsRgxh7dB6COrHnTBNwN0E+lHDAJzu7kLAkDeY08z2/A0hg== @@ -4926,6 +4931,15 @@ union-value@^1.0.0: is-extendable "^0.1.1" set-value "^0.4.3" +universal-analytics@^0.4.20: + version "0.4.20" + resolved "https://registry.yarnpkg.com/universal-analytics/-/universal-analytics-0.4.20.tgz#d6b64e5312bf74f7c368e3024a922135dbf24b03" + integrity sha512-gE91dtMvNkjO+kWsPstHRtSwHXz0l2axqptGYp5ceg4MsuurloM0PU3pdOfpb5zBXUvyjT4PwhWK2m39uczZuw== + dependencies: + debug "^3.0.0" + request "^2.88.0" + uuid "^3.0.0" + unpipe@1.0.0, unpipe@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec" @@ -4986,7 +5000,7 @@ uuid@^2.0.1: resolved "https://registry.yarnpkg.com/uuid/-/uuid-2.0.3.tgz#67e2e863797215530dff318e5bf9dcebfd47b21a" integrity sha1-Z+LoY3lyFVMN/zGOW/nc6/1Hsho= -uuid@^3.3.2: +uuid@^3.0.0, uuid@^3.3.2: version "3.3.2" resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.2.tgz#1b4af4955eb3077c501c23872fc6513811587131" integrity sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==