|
1 | 1 | from posthog import Posthog |
2 | | -import os |
3 | 2 | import pybamm |
4 | 3 | import sys |
5 | 4 |
|
6 | | -_posthog = Posthog( |
7 | | - # this is the public, write only API key, so it's ok to include it here |
8 | | - project_api_key="phc_bLZKBW03XjgiRhbWnPsnKPr0iw0z03fA6ZZYjxgW7ej", |
9 | | - host="https://us.i.posthog.com", |
10 | | -) |
11 | 5 |
|
12 | | -_posthog.log.setLevel("CRITICAL") |
| 6 | +class MockTelemetry: |
| 7 | + def __init__(self): |
| 8 | + self.disabled = True |
13 | 9 |
|
| 10 | + @staticmethod |
| 11 | + def capture(**kwargs): # pragma: no cover |
| 12 | + pass |
14 | 13 |
|
15 | | -def disable(): |
16 | | - _posthog.disabled = True |
| 14 | + |
| 15 | +if pybamm.config.check_opt_out(): |
| 16 | + _posthog = MockTelemetry() |
| 17 | +else: # pragma: no cover |
| 18 | + _posthog = Posthog( |
| 19 | + # this is the public, write only API key, so it's ok to include it here |
| 20 | + project_api_key="phc_bLZKBW03XjgiRhbWnPsnKPr0iw0z03fA6ZZYjxgW7ej", |
| 21 | + host="https://us.i.posthog.com", |
| 22 | + ) |
| 23 | + _posthog.log.setLevel("CRITICAL") |
17 | 24 |
|
18 | 25 |
|
19 | | -_opt_out = os.getenv("PYBAMM_DISABLE_TELEMETRY", "false").lower() |
20 | | -if _opt_out != "false": # pragma: no cover |
21 | | - disable() |
| 26 | +def disable(): |
| 27 | + _posthog.disabled = True |
22 | 28 |
|
23 | 29 |
|
24 | 30 | def capture(event): # pragma: no cover |
25 | | - # don't capture events in automated testing |
26 | 31 | if pybamm.config.is_running_tests() or _posthog.disabled: |
27 | 32 | return |
28 | 33 |
|
29 | | - properties = { |
30 | | - "python_version": sys.version, |
31 | | - "pybamm_version": pybamm.__version__, |
32 | | - } |
| 34 | + if pybamm.config.check_opt_out(): |
| 35 | + disable() |
| 36 | + return |
33 | 37 |
|
34 | 38 | config = pybamm.config.read() |
35 | 39 | if config: |
36 | | - if config["enable_telemetry"]: |
37 | | - user_id = config["uuid"] |
38 | | - _posthog.capture(user_id, event, properties=properties) |
| 40 | + properties = { |
| 41 | + "python_version": sys.version, |
| 42 | + "pybamm_version": pybamm.__version__, |
| 43 | + } |
| 44 | + user_id = config["uuid"] |
| 45 | + _posthog.capture(distinct_id=user_id, event=event, properties=properties) |
0 commit comments