Where can i initialize pluins like Game Analytics and Sentry?

I wanted to initialize Game Analytics and Sentry through code instead of blueprints, but i don’t know where to implement it: here are the lines of code:

Sentry: Unreal Engine | Sentry Documentation

#include "SentrySubsystem.h" 
void Verify()
{
    // Obtain reference to GameInstance
    UGameInstance* GameInstance = ...;

    // Capture message
    USentrySubsystem* SentrySubsystem = GameInstance->GetSubsystem<USentrySubsystem>();
    SentrySubsystem->CaptureMessage(TEXT("Capture message"));
}

and Game Analytics: Unreal - GameAnalytics Documentation

#include "GameAnalytics.h"

UGameAnalytics::initialize("GAME_KEY", "SECRET_KEY");
1 Like

Haven’t used Sentry in Unreal Engine, but for GameAnalytics I first initialize it in my custom game instance subsystem like this:

//#if WITH_GAMEANALYTICS // This is my custom preprocessor defined in Build.cs
#include "Analytics.h"
#include "GameAnalytics.h"
#include "Interfaces/IAnalyticsProvider.h"
//#endif
...
void UMyCustomGameSubsystem::Initialize(FSubsystemCollectionBase& Collection)
{
//#if WITH_GAMEANALYTICS
    const TSharedPtr<IAnalyticsProvider> Provider = FAnalytics::Get().GetDefaultConfiguredProvider();
    if (Provider.IsValid())
    {
        if (Provider->StartSession())
        {
            UGameAnalytics::initialize("KEY", "SECRET");
        }
        else
        {
            UE_LOG(LogTemp, Error, TEXT("Game Analytics failed to initialize!"));
        }
    }
    else
    {
        UE_LOG(LogTemp, Error, TEXT("Failed to get the default analytics provider. Double check [Analytics] configuration in DefaultEngine.ini"));
    }
//#endif
}

Make sure to also have the below entry in DefaultEngine.ini:

[Analytics]
ProviderModuleName=GameAnalytics

Below is an example Build.cs file to define the preprocessor:


public class MyProject : ModuleRules
{
	public MyProject(ReadOnlyTargetRules Target) : base(Target)
	{
		PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
	
		PublicDependencyModuleNames.AddRange(new string[] { "Core" });
		PrivateDependencyModuleNames.AddRange(new string[] {  "CoreUObject", 
			"Engine", 
			"InputCore", 
			"Slate",
			"SlateCore"
		});

		var bIsShippingBuild = Target.Configuration == UnrealTargetConfiguration.Shipping;
		var bCanAddGameAnalytics = bIsShippingBuild && IsGameAnalyticsPluginAvailable();
		PublicDefinitions.Add($"WITH_GAMEANALYTICS={System.Convert.ToInt32(bCanAddGameAnalytics)}");
		if (bCanAddGameAnalytics)
		{
			PublicDependencyModuleNames.Add("GameAnalytics");
			System.Console.WriteLine($"GameAnalytics dependency added for target type {Target.Type.ToString()}");
		}
	}

	public string GetProjectRoot() { return Path.GetFullPath(Path.Combine(ModuleDirectory, "../../")); }
	public bool IsGameAnalyticsPluginAvailable()
	{
		var pluginFile = Path.Combine(GetProjectRoot(), "Plugins", "GameAnalytics", "GameAnalytics.uplugin");
		return File.Exists(pluginFile);
	}
}

1 Like

Thanks!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.