Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Packages/Audience/Runtime/Plugins.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/Packages/Audience/Runtime/Plugins/iOS.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

extern "C" {

const char* _AudienceGetIDFV(void)
{
NSString *idfv = [[UIDevice currentDevice].identifierForVendor UUIDString];
// strdup: IL2CPP calls free() on the returned pointer after copying into a
// managed string. UTF8String is autoreleased (not malloc'd), so free() would
// crash — strdup gives IL2CPP a heap-allocated copy it can safely free.
return idfv ? strdup([idfv UTF8String]) : NULL;
}

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 21 additions & 3 deletions src/Packages/Audience/Runtime/Unity/DeviceCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using Immutable.Audience.Unity.Mobile;
using UnityEngine;

namespace Immutable.Audience.Unity
Expand Down Expand Up @@ -45,11 +46,13 @@ internal static Dictionary<string, object> CollectContext()
return $"{width}x{height}";
}

internal static Dictionary<string, object> CollectGameLaunchProperties()
internal static Dictionary<string, object> CollectGameLaunchProperties(
RuntimePlatform? platformOverride = null)
{
var platform = platformOverride ?? Application.platform;
var props = new Dictionary<string, object>
{
["platform"] = Application.platform.ToString(),
["platform"] = PlatformName(platform),
["version"] = Truncate(Application.version, 256),
["buildGuid"] = Truncate(Application.buildGUID, 256),
["unityVersion"] = Truncate(Application.unityVersion, 256),
Expand All @@ -66,9 +69,18 @@ internal static Dictionary<string, object> CollectGameLaunchProperties()
var dpi = (int)Screen.dpi;
if (dpi > 0) props["screenDpi"] = dpi;

if (Application.platform == RuntimePlatform.Android)
if (platform == RuntimePlatform.Android)
props["androidId"] = Truncate(SystemInfo.deviceUniqueIdentifier, 256);

if (platform == RuntimePlatform.IPhonePlayer)
{
var idfv = IDFVBridge.GetIDFV();
if (idfv != null) props["idfv"] = Truncate(idfv, 256);

// iOS baseline is 163 DPI (1×); 326 → 2×, 401-460 → 3×.
if (dpi > 0) props["screenScale"] = (int)Math.Round(dpi / 163.0);
}

return props;
}

Expand All @@ -92,6 +104,12 @@ internal static Dictionary<string, object> CollectGameLaunchProperties()
}
}

private static string PlatformName(RuntimePlatform platform) => platform switch
{
RuntimePlatform.IPhonePlayer => "iOS",
_ => platform.ToString(),
};

private static string Truncate(string s, int max)
{
if (string.IsNullOrEmpty(s) || s.Length <= max) return s;
Expand Down
8 changes: 8 additions & 0 deletions src/Packages/Audience/Runtime/Unity/Mobile.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions src/Packages/Audience/Runtime/Unity/Mobile/IDFVBridge.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#nullable enable

using System;
#if UNITY_IOS
using System.Runtime.InteropServices;
#endif

namespace Immutable.Audience.Unity.Mobile
{
internal static class IDFVBridge
{
// Replaceable in tests — captures NativeImpl by default.
internal static Func<string?> Impl = NativeImpl;

internal static string? GetIDFV() => Impl();

#if UNITY_IOS
[DllImport("__Internal")]
private static extern string _AudienceGetIDFV();

private static string? NativeImpl() => _AudienceGetIDFV();
#else
private static string? NativeImpl() => null;
#endif
}
}
11 changes: 11 additions & 0 deletions src/Packages/Audience/Runtime/Unity/Mobile/IDFVBridge.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/Packages/Audience/Tests/Runtime/Unity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#nullable enable

using System;
using System.Collections.Generic;
using NUnit.Framework;
using Immutable.Audience.Unity;
using Immutable.Audience.Unity.Mobile;
using UnityEngine;

namespace Immutable.Audience.Tests
{
Expand Down Expand Up @@ -89,5 +92,52 @@ public void CollectGameLaunchProperties_ScreenDpi_AbsentWhenZero()
if (props.TryGetValue("screenDpi", out var dpi))
Assert.Greater((int)dpi, 0, "screenDpi must not be 0 when present");
}

// -----------------------------------------------------------------
// iOS-specific (IDFVBridge + screenScale)
// -----------------------------------------------------------------

private Func<string?>? _originalIDFVImpl;

[SetUp]
public void SetUp() => _originalIDFVImpl = IDFVBridge.Impl;

[TearDown]
public void TearDown() => IDFVBridge.Impl = _originalIDFVImpl!;

[Test]
public void CollectGameLaunchProperties_iOS_IdfvPresentWhenBridgeReturnsValue()
{
IDFVBridge.Impl = () => "12345678-ABCD-ABCD-ABCD-123456789ABC";
var props = DeviceCollector.CollectGameLaunchProperties(RuntimePlatform.IPhonePlayer);
Assert.IsTrue(props.ContainsKey("idfv"), "idfv must be present when bridge returns a value");
Assert.AreEqual("12345678-ABCD-ABCD-ABCD-123456789ABC", props["idfv"]);
}

[Test]
public void CollectGameLaunchProperties_iOS_IdfvAbsentWhenBridgeReturnsNull()
{
IDFVBridge.Impl = () => null;
var props = DeviceCollector.CollectGameLaunchProperties(RuntimePlatform.IPhonePlayer);
Assert.IsFalse(props.ContainsKey("idfv"), "idfv must be absent when bridge returns null");
}

[Test]
public void CollectGameLaunchProperties_NonIOS_DoesNotContainIdfv()
{
// IDFVBridge must never be called on non-iOS platforms.
IDFVBridge.Impl = () => "should-not-appear";
var props = DeviceCollector.CollectGameLaunchProperties();
Assert.IsFalse(props.ContainsKey("idfv"),
"idfv must not be present on non-iOS platforms");
}

[Test]
public void CollectGameLaunchProperties_iOS_ContainsPlatformIPhonePlayer()
{
IDFVBridge.Impl = () => null;
var props = DeviceCollector.CollectGameLaunchProperties(RuntimePlatform.IPhonePlayer);
Assert.AreEqual("iOS", props["platform"]);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading