Google Analytics - Send feature flag analytics to Google Analytics
Sending Feature Flag Evaluation Analytics to Google Analytics
Integrate your feature flag evaluations with Google Analytics experiments to gain advanced insights into how your features are used and to assess the outcomes of A/B tests.
Setup Instructions
- Install SDKs: Add both the ConfigCat SDK and Google Analytics SDK to your application.
- Configure SDKs:
- ConfigCat SDK: Initialize with your ConfigCat SDK key.
- Google Analytics SDK: Set up with your Google Analytics measurement ID.
- Integrate Feature Flag Evaluations:
- During the initialization of the ConfigCat SDK, subscribe to the
flagEvaluated
hook. - Send feature flag evaluation data to Google Analytics using the
experience_impression
event name. Include the following parameter:exp_variant_string
: Construct this string to include the tool name (configcat
), the feature flag's key, and its value. For example:configcat-isMyAwesomeFeatureEnabled-true
.
- During the initialization of the ConfigCat SDK, subscribe to the
Code samples:
- JavaScript, SSR
- React
- Other languages
const configCatClient = configcat.getClient("#YOUR_SDK_KEY", PollingMode.AutoPoll, {
setupHooks: (hooks) =>
hooks.on('flagEvaluated', evaluationDetails => {
const variant = "configcat-" + evaluationDetails.key + "-" + evaluationDetails.value;
gtag('event', 'experience_impression',
{
'exp_variant_string': variant,
'variation_id': evaluationDetails.variationId
});
}),
});
<ConfigCatProvider
sdkKey="#YOUR_SDK_KEY"
pollingMode={PollingMode.AutoPoll}
options={{
setupHooks: (hooks) =>
hooks.on('flagEvaluated', evaluationDetails => {
const variant = "configcat-" + evaluationDetails.key + "-" + evaluationDetails.value;
gtag('event', 'experience_impression',
{
'exp_variant_string': variant,
'variation_id': evaluationDetails.variationId
});
}),
}}
>
</ConfigCatProvider>
While our documentation primarily provides code examples for JavaScript-based SDKs, you can integrate with other languages by utilizing the Send Measurement Protocol events to Google Analytics endpoint. Here’s how to set it up:
- Subscribe to the FlagEvaluated hook in the ConfigCat SDK.
- Send an event to Google Analytics using the
experience_impression
event name. Include the following event properties:exp_variant_string
: Format this string asconfigcat-<feature_flag_key>-<feature_flag_value>
. For example,configcat-isMyAwesomeFeatureEnabled-true
.variant_id
: The evaluated feature flag's value or the variationId from the FlagEvaluated hook's EvaluationDetails.user_id
(optional): If tracking in a backend component and utilizing the user_id feature in Google Analytics, include this property to identify your user. Use the Identifier property from the User object in the FlagEvaluated hook, or a value that best describes your user.
For Text feature flags with lengthy values (e.g., JSON), send the variationId
instead of the value
as the exp_variant_string
to Google Analytics. The variationId
is a hashed version of the feature flag value, accessible on the ConfigCat Dashboard by enabling the Show VariationIDs to support A/B testing setting. Learn more here.
-
Deploy your application and wait for feature flag evaluations to occur. This process might take 1-2 days for the
experience_impression
events to populate in Google Analytics. -
Define an audience in Google Analytics using the
exp_variant_string
parameter with the same values that you using in your events (e.g.configcat-isMyAwesomeFeatureEnabled-true
) it by following the guide here. This allows you to leverage feature flag evaluation events effectively.