unified_analytics.dart 2.91 KB
Newer Older
1 2 3 4 5 6
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:unified_analytics/unified_analytics.dart';

7
import '../base/config.dart';
8
import '../base/io.dart';
9
import '../features.dart';
10
import '../globals.dart' as globals;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
import '../version.dart';

/// This function is called from within the context runner to perform
/// checks that are necessary for determining if a no-op version of
/// [Analytics] gets returned.
///
/// When [enableAsserts] is set to `true`, various assert statements
/// will be enabled to ensure usage of this class is within GA4 limitations.
///
/// For testing purposes, pass in a [FakeAnalytics] instance initialized with
/// an in-memory [FileSystem] to prevent writing to disk.
Analytics getAnalytics({
  required bool runningOnBot,
  required FlutterVersion flutterVersion,
  required Map<String, String> environment,
26
  required String? clientIde,
27
  required Config config,
28 29 30 31 32 33 34 35 36 37 38 39 40 41
  bool enableAsserts = false,
  FakeAnalytics? analyticsOverride,
}) {
  final String version = flutterVersion.getVersionString(redactUnknownBranches: true);
  final bool suppressEnvFlag = environment['FLUTTER_SUPPRESS_ANALYTICS']?.toLowerCase() == 'true';

  if (// Ignore local user branches.
      version.startsWith('[user-branch]') ||
      // Many CI systems don't do a full git checkout.
      version.endsWith('/unknown') ||
      // Ignore bots.
      runningOnBot ||
      // Ignore when suppressed by FLUTTER_SUPPRESS_ANALYTICS.
      suppressEnvFlag) {
42
    return const NoOpAnalytics();
43 44 45 46 47 48 49 50 51 52 53 54 55 56
  }

  // Providing an override of the [Analytics] instance is preferred when
  // running tests for this function to prevent writing to the filesystem
  if (analyticsOverride != null) {
    return analyticsOverride;
  }

  return Analytics(
    tool: DashTool.flutterTool,
    flutterChannel: flutterVersion.channel,
    flutterVersion: flutterVersion.frameworkVersion,
    dartVersion: flutterVersion.dartSdkVersion,
    enableAsserts: enableAsserts,
57
    clientIde: clientIde,
58
    enabledFeatures: getEnabledFeatures(config),
59 60
  );
}
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75
/// Uses the [Config] object to get enabled features.
String? getEnabledFeatures(Config config) {
  // Create string with all enabled features to send as user property
  final Iterable<Feature> enabledFeatures = allFeatures.where((Feature feature) {
    final String? configSetting = feature.configSetting;
    return configSetting != null && config.getValue(configSetting) == true;
  });
  return enabledFeatures.isNotEmpty
      ? enabledFeatures
          .map((Feature feature) => feature.configSetting)
          .join(',')
      : null;
}

76 77 78 79 80 81 82 83 84
/// Function to safely grab the max rss from [ProcessInfo].
int? getMaxRss(ProcessInfo processInfo) {
  try {
    return globals.processInfo.maxRss;
  } on Exception catch (error) {
    globals.printTrace('Querying maxRss failed with error: $error');
  }
  return null;
}