...

/

Solution: Create a Feature-Flagged Analytics Service

Solution: Create a Feature-Flagged Analytics Service

Use a flag-to-class map to instantiate the correct service: real or stub; without any conditionals.

We'll cover the following...

Solution explanation

  • Lines 1–16: We define two interchangeable service classes: AnalyticsService and StubService.

    • AnalyticsService logs events and generates reports when analytics is enabled.

    • StubService provides empty .track() and .report() methods so calls remain safe even when analytics is disabled.

  • Lines 18–21: The serviceMap object maps the analyticsEnabled ...