{"id":18478536,"url":"https://github.com/growthbook/growthbook-elixir","last_synced_at":"2025-07-04T13:38:47.187Z","repository":{"id":234005608,"uuid":"782356873","full_name":"growthbook/growthbook-elixir","owner":"growthbook","description":"GrowthBook SDK for Elixir","archived":false,"fork":false,"pushed_at":"2025-03-18T22:50:39.000Z","size":103,"stargazers_count":6,"open_issues_count":7,"forks_count":1,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-03-18T23:21:43.849Z","etag":null,"topics":["ab-testing","elixir","feature-flags","sdk"],"latest_commit_sha":null,"homepage":"https://www.growthbook.io","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/growthbook.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-05T06:22:13.000Z","updated_at":"2024-09-11T15:39:13.000Z","dependencies_parsed_at":null,"dependency_job_id":"b34ee615-c5e0-4d9d-87f6-e7ed4e29e763","html_url":"https://github.com/growthbook/growthbook-elixir","commit_stats":null,"previous_names":["growthbook/growthbook-elixir"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/growthbook%2Fgrowthbook-elixir","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/growthbook%2Fgrowthbook-elixir/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/growthbook%2Fgrowthbook-elixir/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/growthbook%2Fgrowthbook-elixir/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/growthbook","download_url":"https://codeload.github.com/growthbook/growthbook-elixir/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247871467,"owners_count":21010047,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ab-testing","elixir","feature-flags","sdk"],"created_at":"2024-11-06T12:10:28.536Z","updated_at":"2025-04-08T15:32:36.749Z","avatar_url":"https://github.com/growthbook.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GrowthBook - Elixir SDK\n\n![Elixir SDK for the GrowthBook feature flagging and AB testing platform](https://docs.growthbook.io/images/GrowthBook-hero-elixir.png)\n\n[Online documentation](https://hexdocs.pm/growthbook) | [Hex.pm](https://hex.pm/packages/growthbook)\n\n\u003c!-- MDOC !--\u003e\n\n`GrowthBook` is a [GrowthBook](https://growthbook.io) SDK for Elixir/OTP.\n\nThis SDK follows the guidelines set out in [GrowthBook's documentation](https://docs.growthbook.io/lib/build-your-own), and the API is tested on conformance with the test cases from the JS SDK.\n\nTo ensure an Elixir-friendly API, the implementation deviates from the official SDK in the following ways:\n\n- Instead of tuple-lists, this library uses actual tuples\n- Comparisons with `undefined` are implemented by using `:undefined`\n- Function names are converted to `snake_case`, and `is_` prefix is replaced with a `?` suffix\n- Instead of classes, a Context struct is used (similar to `%Plug.Conn{}` in `plug`)\n\n## What is GrowthBook?\n\n[GrowthBook](https://www.growthbook.io) is an open source A/B testing platform. The platform works\nsignificantly different from other A/B testing platforms, most notably: it is language agnostic.\n\nClients by default work offline, and manage their own data. This means that you are free to\nimplement A/B tests server-side, or client-side without worrying about things like \"anti-flicker\"\nscripts, or the added latency of JS embeds.\n\nFurthermore, GrowthBook supports both experiments (A/B tests and multivariate tests) and feature\nflags. Because all logic to run experiments and feature flags is contained in the library, there\nis virtually no added latency to running experiments or using feature flags.\n\n## Installation\n\nAdd `growthbook` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:growthbook, \"~\u003e 0.2\"}\n  ]\nend\n```\n\n## Usage\n\n```elixir\n# Create a context, which can be reused for multiple users\n# Approach 1: Manual Feature Configuration\nfeatures_config = Jason.decode!(\"\"\"\n{\n  \"features\": {\n    \"send-reminder\": {\n      \"defaultValue\": false,\n      \"rules\": [{ \"condition\": { \"browser\": \"chrome\" }, \"force\": true }]\n    },\n    \"add-to-cart-btn-color\": {\n      \"rules\": [{ \"variations\": [{ \"color\": \"red\" }, { \"color\": \"green\" }] }]\n    }\n  }\n}\n\"\"\")\n\nfeatures = GrowthBook.Config.features_from_config(features_config)\n\ncontext = GrowthBook.build_context(\n  %{\n    \"id\" =\u003e \"12345\",\n    \"country_code\" =\u003e \"NL\",\n    \"browser\" =\u003e \"chrome\"\n  },\n  features  # Explicitly provide features\n)\n\n# Approach 2: Auto-refreshing Features\ncase GrowthBook.init(\n  client_key: \"key_prod_123...\",\n  api_host: \"https://cdn.growthbook.io\",\n  decryption_key: \"key_123...\",\n  swr_ttl_seconds: 60,\n  on_refresh: fn features -\u003e\n    Logger.info(\"Features refreshed: #{map_size(features)} available\")\n    MyApp.FeatureNotifier.features_updated(features)\n  end\n) do\n  {:ok, :initialized} -\u003e # Features loaded successfully\n    # Start using GrowthBook...\n  {:error, reason} -\u003e # Handle error\n    # Handle initialization failure...\nend\n\n# Create context that automatically uses latest features\ncontext = GrowthBook.build_context(%{\n  \"id\" =\u003e \"12345\",\n  \"country_code\" =\u003e \"NL\",\n  \"browser\" =\u003e \"chrome\"\n})\n\n# Usage is the same for both approaches:\n\n# Use a feature toggle\nif GrowthBook.feature(context, \"send-reminder\").on? do\n  Logger.info(\"Sending reminder\")\nend\n\n# Use a feature's value\ncolor = GrowthBook.feature(context, \"add-to-cart-btn-color\").value[\"color\"]\nLogger.info(\"Color: \" \u003c\u003e color)\n\n# Run an inline experiment\nif GrowthBook.run(context, %GrowthBook.Experiment{\n  key: \"checkout-v2\",\n  active?: true,\n  coverage: 1,\n  variations: [1, 2]\n}).in_experiment? do\n  Logger.info(\"In experiment\")\nend\n```\n\n## Auto-Refresh Features\n\nGrowthBook now supports automatic feature refreshing from your GrowthBook API. Instead of manually managing features, you can initialize GrowthBook with your API credentials.\n\n### Core Components\n\n1. GenServer-Based Repository: Implemented as a GenServer that maintains feature state in memory (with stale-while-revalidate (SWR) caching strategy) that handles fetching, caching of features and refresh cycles based on configuration.\n\n```elixir\n# Initialize GrowthBook with auto-refresh\ncase GrowthBook.init(\n  client_key: \"key_prod_123...\",\n  api_host: \"https://cdn.growthbook.io\",\n  decryption_key: \"key_123...\",  # Optional: Required for encrypted features\n  swr_ttl_seconds: 60,  # Optional: Default is 60 seconds\n  refresh_strategy: :periodic,  # Optional: :periodic (default) or :manual\n  on_refresh: fn features -\u003e  # Optional: Callback when features are refreshed\n    Logger.info(\"Features refreshed: #{map_size(features)} available\")\n    # Notify your application about new features\n    MyApp.FeatureNotifier.features_updated(features)\n  end\n) do\n  {:ok, :initialized} -\u003e # Features loaded successfully\n    # Start using GrowthBook...\n  {:error, reason} -\u003e # Handle error\n    # Handle initialization failure...\nend\n\n# Create context that automatically uses latest features\ncontext = GrowthBook.build_context(%{\n  \"id\" =\u003e \"12345\",\n  \"country_code\" =\u003e \"NL\",\n  \"browser\" =\u003e \"chrome\"\n})\n\n# Features will automatically refresh in the background\n# All feature evaluations will use the latest features\nif GrowthBook.feature(context, \"send-reminder\").on? do\n  Logger.info(\"Sending reminder\")\nend\n```\n\n### Refresh Strategies\n\nThe SDK supports two refresh strategies:\n\n- `:periodic` (default): Features are automatically refreshed in the background based on the TTL.\n- `:manual`: Features are only refreshed when explicitly called with `GrowthBook.FeatureRepository.refresh()`.\n\n### Shutting Down\n\nWhen your application is shutting down or you no longer need the feature repository, stop it properly to release resources:\n\n```elixir\n# Stops the FeatureRepository GenServer\nGenServer.stop(GrowthBook.FeatureRepository)\n```\n\n### Supervision\n\nSince the FeatureRepository is a GenServer, it's recommended to add it to your application's supervision tree for proper lifecycle management:\n\n```elixir\n# In your application.ex\ndef start(_type, _args) do\n  children = [\n    # Other children...\n    \n    # Add GrowthBook FeatureRepository to supervision tree\n    {GrowthBook.FeatureRepository, \n      client_key: \"key_prod_123...\",\n      api_host: \"https://cdn.growthbook.io\",\n      # Add other options as needed\n    }\n  ]\n\n  opts = [strategy: :one_for_one, name: MyApp.Supervisor]\n  Supervisor.start_link(children, opts)\n  \n  # Then initialize GrowthBook\n  case GrowthBook.FeatureRepository.await_initialization(GrowthBook.FeatureRepository, 5000) do\n    :ok -\u003e \n      Logger.info(\"GrowthBook features initialized successfully\")\n    {:error, reason} -\u003e \n      Logger.error(\"Failed to initialize GrowthBook features: #{reason}\")\n  end\n  \n  {:ok, self()}\nend\n```\n\nKey improvements:\n\n- **Auto-refresh**: Features automatically refresh from your GrowthBook API\n- **Encryption**: Support for encrypted feature payloads\n- **Callbacks**: Get notified when features are updated\n- **Resilient**: Keeps existing features on API errors\n- **Configurable**: Control refresh intervals and strategies\n\n## License\n\nThis library is MIT licensed. See the\n[LICENSE](https://github.com/growthbook/growthbook-elixir/blob/main/LICENSE)\nfile in this repository for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrowthbook%2Fgrowthbook-elixir","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgrowthbook%2Fgrowthbook-elixir","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrowthbook%2Fgrowthbook-elixir/lists"}