Skip to content

Commit

Permalink
Add telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Maksimov committed Jan 19, 2024
1 parent 429026f commit 63f6892
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 8 deletions.
10 changes: 10 additions & 0 deletions data/com.github.tenderowl.frog.gschema.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<schemalist gettext-domain="frog">
<schema id="com.github.tenderowl.frog" path="/com/github/tenderowl/frog/">
<key name="installation-id" type="s">
<default>""</default>
<summary>App Installation ID</summary>
<description>Unique installation identifier.</description>
</key>
<key name="telemetry" type="b">
<default>true</default>
<summary>Telemetry activation</summary>
<description>Telemetry activate status.</description>
</key>
<!-- Window params -->
<key name="window-width" type="i">
<default>-1</default>
Expand Down
1 change: 1 addition & 0 deletions flatpak/com.github.tenderowl.frog.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"tesseract.json",
"libportal.json",
"zbar.json",
"python3-nanoid.json",
"python3-loguru.json",
"python3-pytesseract.json",
"python3-pyzbar.json",
Expand Down
14 changes: 14 additions & 0 deletions flatpak/python3-nanoid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "python3-nanoid",
"buildsystem": "simple",
"build-commands": [
"pip3 install --verbose --exists-action=i --no-index --find-links=\"file://${PWD}\" --prefix=${FLATPAK_DEST} \"nanoid\" --no-build-isolation"
],
"sources": [
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/2e/0d/8630f13998638dc01e187fadd2e5c6d42d127d08aeb4943d231664d6e539/nanoid-2.0.0-py3-none-any.whl",
"sha256": "90aefa650e328cffb0893bbd4c236cfd44c48bc1f2d0b525ecc53c3187b653bb"
}
]
}
29 changes: 27 additions & 2 deletions frog/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@
import sys
from gettext import gettext as _

import nanoid
from gi.repository import Gtk, Gio, GLib, Notify, Adw, GdkPixbuf, Gdk, GObject
from loguru import logger

from frog.config import RESOURCE_PREFIX, APP_ID
from frog.language_manager import language_manager
from frog.services.clipboard_service import clipboard_service
from frog.services.posthog import posthog
from frog.services.screenshot_service import ScreenshotService
from frog.services.telemetry import telemetry
from frog.settings import Settings
from frog.window import FrogWindow

Expand All @@ -47,6 +48,7 @@ class FrogApplication(Adw.Application):
gtk_settings: Gtk.Settings

settings: Settings = GObject.Property(type=GObject.TYPE_PYOBJECT)
installation_id: str = GObject.Property(type=str)

def __init__(self, version=None):
super().__init__(application_id=APP_ID,
Expand All @@ -57,6 +59,9 @@ def __init__(self, version=None):
# Init GSettings
self.settings = Settings.new()

telemetry.set_is_active(self.settings.get_boolean('telemetry'))
self.ensure_installation_id()

self.add_main_option(
'extract_to_clipboard',
ord('e'),
Expand Down Expand Up @@ -123,16 +128,29 @@ def do_command_line(self, command_line):
self.activate()
return 0

def ensure_installation_id(self):
self.installation_id = self.settings.get_string("installation-id")
if not self.installation_id:
logger.info("No installation id was found. Generating a new one.")
self.installation_id = nanoid.generate()
self.settings.set_string("installation-id", self.installation_id)
telemetry.set_installation_id(self.installation_id)
telemetry.capture('new Installation ID generated')

telemetry.set_installation_id(self.installation_id)

def on_preferences(self, _action, _param) -> None:
telemetry.capture('preferences activated')
self.get_active_window().show_preferences()

def on_github_star(self, _action, _param) -> None:
posthog.capture('github_star', 'github_star_activated')
telemetry.capture('star github activated')
launcher: Gtk.UriLauncher = Gtk.UriLauncher()
launcher.set_uri('https://github.com/TenderOwl/Frog')
launcher.launch(callback=self._on_github_star)

def on_about(self, _action, _param):
telemetry.capture('about activated')
about_window = Adw.AboutWindow(
application_name="Frog",
application_icon=APP_ID,
Expand All @@ -158,27 +176,34 @@ def on_about(self, _action, _param):
about_window.present()

def on_shortcuts(self, _action, _param):
telemetry.capture('shortcuts activated')
builder = Gtk.Builder()
builder.add_from_resource(f"{RESOURCE_PREFIX}/ui/shortcuts.ui")
builder.get_object("shortcuts").set_transient_for(self.get_active_window())
builder.get_object("shortcuts").present()

def on_copy_to_clipboard(self, _action, _param) -> None:
telemetry.capture('copy_to_clipboard activated')
self.get_active_window().on_copy_to_clipboard(self)

def on_show_uri(self, _action, param) -> None:
telemetry.capture('show_uri activated')
Gtk.show_uri(None, param.get_string(), Gdk.CURRENT_TIME)

def get_screenshot(self, _action, _param) -> None:
telemetry.capture('screenshot activated')
self.get_active_window().get_screenshot()

def get_screenshot_and_copy(self, _action, _param) -> None:
telemetry.capture('screenshot_and_copy activated')
self.get_active_window().get_screenshot(copy=True)

def open_image(self, _action, _param) -> None:
telemetry.capture('open_image activated')
self.get_active_window().open_image()

def on_paste_from_clipboard(self, _action, _param) -> None:
telemetry.capture('paste_from_clipboard activated')
self.get_active_window().on_paste_from_clipboard(self)

@staticmethod
Expand Down
4 changes: 4 additions & 0 deletions frog/services/clipboard_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
from gi.repository import Gdk, GObject, Gio
from loguru import logger

from frog.services.telemetry import telemetry


class ClipboardService(GObject.GObject):
__gtype_name__ = 'ClipboardService'
Expand All @@ -47,6 +49,7 @@ def __init__(self):

def set(self, value: str) -> None:
self.clipboard.set(value)
telemetry.capture('clipboard set')

def _on_read_texture(self, _sender: GObject.GObject, result: Gio.AsyncResult) -> None:
try:
Expand All @@ -57,6 +60,7 @@ def _on_read_texture(self, _sender: GObject.GObject, result: Gio.AsyncResult) ->
self.emit('paste_from_clipboard', texture)

def read_texture(self) -> None:
telemetry.capture('clipboard read texture')
self.clipboard.read_texture_async(cancellable=None,
callback=self._on_read_texture)

Expand Down
3 changes: 0 additions & 3 deletions frog/services/posthog.py

This file was deleted.

3 changes: 3 additions & 0 deletions frog/services/screenshot_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from loguru import logger

from frog.config import tessdata_config
from frog.services.telemetry import telemetry

try:
from PIL import Image
Expand Down Expand Up @@ -82,6 +83,7 @@ def capture(self, lang: str, copy: bool = False) -> None:
If image is not recognized, returns None.
"""
telemetry.capture('screenshot capture', {'language': lang})
self.portal.take_screenshot(
None,
Xdp.ScreenshotFlags.INTERACTIVE,
Expand Down Expand Up @@ -151,4 +153,5 @@ def decode_image(self,
self.emit("error", "No text found.")

def capture_cancelled(self, cancellable: Gio.Cancellable) -> None:
telemetry.capture('screenshot cancelled')
self.emit("error", "Cancelled")
3 changes: 3 additions & 0 deletions frog/services/share_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
from gi.repository import GObject, Gtk
from loguru import logger

from frog.services.telemetry import telemetry


class ShareService(GObject.GObject):
__gtype_name__ = "ShareService"
Expand All @@ -55,6 +57,7 @@ def providers() -> List[str]:
]

def share(self, provider: str, text: str):
telemetry.capture("share", {'provider': provider})
if not text:
return

Expand Down
30 changes: 30 additions & 0 deletions frog/services/telemetry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from typing import Any

from gi.repository import GObject
from posthog import Posthog


class TelemetryService(GObject.GObject):
_gtype_name = 'TelemetryService'

posthog: Posthog | None
installation_id: str | None
is_active: bool = True

def __init__(self):
super().__init__()
self.posthog = Posthog(project_api_key='phc_HpETCN6yQKZIr8gr6mBQTd3H0SjKUBrNMI3AizoX97f',
host='https://eu.posthog.com')

def set_installation_id(self, installation_id: str):
self.installation_id = installation_id

def set_is_active(self, is_active: bool):
self.is_active = is_active

def capture(self, event: str, props: Any = None):
if self.posthog and self.is_active:
self.posthog.capture(self.installation_id, event, props)


telemetry = TelemetryService()
5 changes: 3 additions & 2 deletions frog/widgets/language_popover.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

from frog.config import RESOURCE_PREFIX
from frog.language_manager import language_manager
from frog.services.posthog import posthog
from frog.services.telemetry import telemetry
from frog.settings import Settings
from frog.types.language_item import LanguageItem
from frog.widgets.language_popover_row import LanguagePopoverRow
Expand Down Expand Up @@ -96,6 +96,7 @@ def _on_language_removed(self, _sender, _lang_code: str):

@Gtk.Template.Callback()
def _on_search_activate(self, entry: Gtk.SearchEntry):
telemetry.capture('language_search activated')
self._on_language_activate(self.list_view, 0)

@Gtk.Template.Callback()
Expand All @@ -104,7 +105,7 @@ def _on_language_activate(self, _: Gtk.ListBox, row: LanguagePopoverRow):
self.emit('language-changed', item)
self.active_language = item.code
language_manager.active_language = item
posthog.capture(item.code, 'language-activated')
telemetry.capture('language-activated', {'language': self.active_language})
self.popdown()

@Gtk.Template.Callback()
Expand Down
5 changes: 4 additions & 1 deletion frog/widgets/preferences_general_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# holders shall not be used in advertising or otherwise to promote the sale,
# use or other dealings in this Software without prior written
# authorization.

import posthog
from gi.repository import Gtk, Adw, Gio
from loguru import logger

Expand Down Expand Up @@ -58,6 +58,9 @@ def __init__(self):
self.extra_language_combo.set_selected(extra_language_index)
self.extra_language_combo.connect('notify::selected-item', self._on_extra_language_changed)

def do_show(self, *args, **kwargs):
posthog.capture('', 'preferences general page opened')

def _on_extra_language_changed(self, combo_row: Adw.ComboRow, _param):
lang_name = combo_row.get_selected_item().get_string()
lang_code = language_manager.get_language_code(lang_name)
Expand Down

0 comments on commit 63f6892

Please sign in to comment.