def ready(self): posthoganalytics.api_key = "sTMFPsFhdP1Ssg" posthoganalytics.personal_api_key = os.environ.get( "POSTHOG_PERSONAL_API_KEY") # Skip plugin sync in manage.py scripts and in tests # (the database tables might not yet be created) if (not settings.TEST and not "makemigrations" in sys.argv and not "migrate" in sys.argv and not "manage.py" in " ".join(sys.argv) and not "/mypy" in sys.argv[0]): from posthog.plugins import sync_plugin_config # syncs posthog.json['plugins'] and the Plugin/PluginConfig models sync_plugin_config() if settings.DEBUG: # log development server launch to posthog if os.getenv("RUN_MAIN") == "true": posthoganalytics.capture( get_machine_id(), "development server launched", { "posthog_version": VERSION, "git_rev": get_git_commit(), "git_branch": get_git_branch(), }, ) posthoganalytics.disabled = True elif settings.TEST or os.environ.get("OPT_OUT_CAPTURE"): posthoganalytics.disabled = True
def capture_event(name: str, report: Dict[str, Any], dry_run: bool) -> None: if not dry_run: posthoganalytics.api_key = "sTMFPsFhdP1Ssg" disabled = posthoganalytics.disabled posthoganalytics.disabled = False posthoganalytics.capture(get_machine_id(), "instance status report", report) posthoganalytics.disabled = disabled
def fetch_instance_params(report: Dict[str, Any]) -> dict: return { "site_url": settings.SITE_URL, "machine_id": get_machine_id(), "posthog_version": report["posthog_version"], "deployment": report["deployment"], "realm": report["realm"], **report["instance_usage_summary"], }
def _capture(metric_name: str, value: Any, tags: Tags): from posthog.api.capture import capture_internal team_id = get_internal_metrics_team_id() if team_id is not None: now = timezone.now() distinct_id = utils.get_machine_id() event = {"event": f"$${metric_name}", "properties": {"value": value, **(tags or {})}} capture_internal(event, distinct_id, None, None, now, now, team_id)
def ready(self): posthoganalytics.api_key = "sTMFPsFhdP1Ssg" if settings.DEBUG: # log development server launch to posthog if os.getenv("RUN_MAIN") == "true": posthoganalytics.capture(get_machine_id(), "development server launched") posthoganalytics.disabled = True elif settings.TEST or os.environ.get("OPT_OUT_CAPTURE"): posthoganalytics.disabled = True
def capture_event(name: str, report: Dict[str, Any], dry_run: bool) -> None: if not dry_run: posthoganalytics.api_key = "sTMFPsFhdP1Ssg" posthoganalytics.capture(get_machine_id(), name, {**report, "scope": "machine"}, groups={"instance": SITE_URL}) if "instance_usage_summary" in report: posthoganalytics.group_identify("instance", SITE_URL, fetch_instance_params(report)) for user in User.objects.all(): posthoganalytics.capture(user.distinct_id, f"user {name}", {**report, "scope": "user"}) else: print(name, json.dumps(report)) # noqa: T001
def capture_event(name: str, report: Dict[str, Any], dry_run: bool) -> None: if not dry_run: posthoganalytics.api_key = "sTMFPsFhdP1Ssg" posthoganalytics.capture(get_machine_id(), name, { **report, "scope": "machine" }) for user in User.objects.all(): posthoganalytics.capture(user.distinct_id, f"user {name}", { **report, "scope": "user" }) else: print(name, json.dumps(report)) # noqa: T001
def ready(self): posthoganalytics.api_key = "sTMFPsFhdP1Ssg" if settings.DEBUG: # log development server launch to posthog if os.getenv("RUN_MAIN") == "true": posthoganalytics.capture( get_machine_id(), "development server launched", {"posthog_version": VERSION, "git_rev": get_git_commit(), "git_branch": get_git_branch(),}, ) posthoganalytics.disabled = True elif settings.TEST or os.environ.get("OPT_OUT_CAPTURE"): posthoganalytics.disabled = True
def handle(self, *args, **options): report = get_helm_info_env() report["posthog_version"] = VERSION report["deployment"] = os.getenv("DEPLOYMENT", "unknown") print(f"Report for {get_machine_id()}:") pprint(report) if not options["dry_run"]: posthoganalytics.api_key = "sTMFPsFhdP1Ssg" disabled = posthoganalytics.disabled posthoganalytics.disabled = False posthoganalytics.capture(get_machine_id(), "helm_install", report) posthoganalytics.disabled = disabled
def ready(self): posthoganalytics.api_key = "sTMFPsFhdP1Ssg" posthoganalytics.personal_api_key = os.environ.get( "POSTHOG_PERSONAL_API_KEY") if settings.TEST or os.environ.get("OPT_OUT_CAPTURE", False): posthoganalytics.disabled = True elif settings.DEBUG: # log development server launch to posthog if os.getenv("RUN_MAIN") == "true": # Sync all organization.available_features once on launch, in case plans changed from posthog.celery import sync_all_organization_available_features sync_all_organization_available_features() posthoganalytics.capture( get_machine_id(), "development server launched", { "posthog_version": VERSION, "git_rev": get_git_commit(), "git_branch": get_git_branch(), }, ) if SELF_CAPTURE: posthoganalytics.api_key = get_self_capture_api_token(None) else: posthoganalytics.disabled = True if not settings.SKIP_SERVICE_VERSION_REQUIREMENTS: for service_version_requirement in settings.SERVICE_VERSION_REQUIREMENTS: in_range, version = service_version_requirement.is_service_in_accepted_version( ) if not in_range: print( f"\033[91mService {service_version_requirement.service} is in version {version}. Expected range: {str(service_version_requirement.supported_version)}. PostHog may not work correctly with the current version. To continue anyway, add SKIP_SERVICE_VERSION_REQUIREMENTS=1 as an environment variable\033[0m", ) exit(1) from posthog.async_migrations.setup import setup_async_migrations if SKIP_ASYNC_MIGRATIONS_SETUP: print_warning([ "Skipping async migrations setup. This is unsafe in production!" ]) else: setup_async_migrations()
def _capture(metric_name: str, value: Any, tags: Tags): from posthog.api.capture import capture_internal try: team_id = get_internal_metrics_team_id() if team_id is not None: now = timezone.now() distinct_id = utils.get_machine_id() event = { "event": f"$${metric_name}", "properties": { "value": value, **(tags or {}) } } capture_internal(event, distinct_id, None, None, now, now, team_id) except Exception as err: # Ignore errors, this is not important enough to fail API on capture_exception(err)
def status_report() -> None: period_start, period_end = get_previous_week() report: Dict[str, Any] = { "posthog_version": VERSION, "period": { "start_inclusive": period_start.isoformat(), "end_inclusive": period_end.isoformat() }, } report["users_who_logged_in"] = [{ "id": user.id, "distinct_id": user.distinct_id } if user.anonymize_data else { "id": user.id, "distinct_id": user.distinct_id, "first_name": user.first_name, "email": user.email } for user in User.objects.filter(last_login__gte=period_start)] report["teams"] = {} for team in Team.objects.all(): team_report: Dict[str, Any] = {} events_considered_total = Event.objects.filter(team_id=team.id) events_considered_new_in_period = events_considered_total.filter( created_at__gte=period_start, created_at__lte=period_end, ) persons_considered_total = Event.objects.filter(team_id=team.id) persons_considered_total_new_in_period = persons_considered_total.filter( created_at__gte=period_start, created_at__lte=period_end, ) team_report["events_count_total"] = events_considered_total.count() team_report[ "events_count_new_in_period"] = events_considered_new_in_period.count( ) team_report["persons_count_total"] = persons_considered_total.count() team_report[ "persons_count_new_in_period"] = persons_considered_total_new_in_period.count( ) with connection.cursor() as cursor: cursor.execute( sql.SQL(""" SELECT COUNT(DISTINCT person_id) as persons_count FROM posthog_event JOIN posthog_persondistinctid ON (posthog_event.distinct_id = posthog_persondistinctid.distinct_id) WHERE posthog_event.team_id = %s AND posthog_event.created_at >= %s AND posthog_event.created_at < %s """), (team.id, report["period"]["start_inclusive"], report["period"]["end_exclusive"]), ) team_report["persons_count_active_in_period"] = cursor.fetchone( )[0] cursor.execute( sql.SQL(""" SELECT properties->>'$lib' as lib, COUNT(*) as count FROM posthog_event WHERE team_id = %s AND created_at >= %s AND created_at < %s GROUP BY lib """), (team.id, report["period"]["start_inclusive"], report["period"]["end_exclusive"]), ) team_report["events_count_by_lib"] = { result.lib: result.count for result in namedtuplefetchall(cursor) } cursor.execute( sql.SQL(""" SELECT event as name, COUNT(*) as count FROM posthog_event WHERE team_id = %s AND created_at >= %s AND created_at < %s GROUP BY name """), (team.id, report["period"]["start_inclusive"], report["period"]["end_exclusive"]), ) team_report["events_count_by_name"] = { result.name: result.count for result in namedtuplefetchall(cursor) } report["teams"][team.id] = team_report posthoganalytics.api_key = "sTMFPsFhdP1Ssg" disabled = posthoganalytics.disabled posthoganalytics.disabled = False posthoganalytics.capture(get_machine_id(), "instance status report", report) posthoganalytics.disabled = disabled
def fetch_instance_params(report: Dict[str, Any]) -> dict: return {"site_url": SITE_URL, "machine_id": get_machine_id(), **report["instance_usage_summary"]}