Skip to content

Commit

Permalink
Add basic analytics through universal-analytics #4
Browse files Browse the repository at this point in the history
  • Loading branch information
rohit-gohri committed Mar 3, 2019
1 parent 978dbc2 commit 2e4ecfd
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
},
"telegram": {
"token": "xxxx"
}
},
"trackingId": "UA-xxxxxxxxx-x"
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
31 changes: 30 additions & 1 deletion src/lib/telegram.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -93,14 +93,43 @@ 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,
// is_personal: true,
});
});

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}`);
})

Expand Down
20 changes: 17 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down Expand Up @@ -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==
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down

0 comments on commit 2e4ecfd

Please sign in to comment.