Skip to content

Commit 9526027

Browse files
Merge pull request #412 from OneSignal/feature/vsp
Add VSP to 2.x.x and exclude from locally generated package
2 parents 370febd + 55c11a3 commit 9526027

File tree

9 files changed

+187
-13
lines changed

9 files changed

+187
-13
lines changed

OneSignalExample/Assets/OneSignal/Attribution.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "OneSignal.UnityPackage.Attribution",
3+
"rootNamespace": "",
4+
"references": [
5+
"OneSignal.Core"
6+
],
7+
"includePlatforms": [
8+
"Android",
9+
"Editor",
10+
"iOS"
11+
],
12+
"excludePlatforms": [],
13+
"allowUnsafeCode": false,
14+
"overrideReferences": false,
15+
"precompiledReferences": [],
16+
"autoReferenced": true,
17+
"defineConstraints": [],
18+
"versionDefines": [
19+
{
20+
"name": "com.onesignal.unity.core",
21+
"expression": "0.0.1-preview",
22+
"define": "ONE_SIGNAL_INSTALLED"
23+
}
24+
],
25+
"noEngineReferences": false
26+
}

OneSignalExample/Assets/OneSignal/Attribution/OneSignal.UnityPackage.Attribution.asmdef.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
using UnityEditor;
3+
using UnityEngine;
4+
using UnityEngine.Analytics;
5+
6+
namespace OneSignalSDK
7+
{
8+
internal static class VspAttribution
9+
{
10+
const int k_MaxEventsPerHour = 1000;
11+
const int k_MaxNumberOfElements = 1000;
12+
13+
const string k_VendorKey = "unity.vsp-attribution";
14+
const string k_EventName = "vspAttribution";
15+
16+
static bool RegisterEvent()
17+
{
18+
#if UNITY_EDITOR
19+
AnalyticsResult result = EditorAnalytics.RegisterEventWithLimit(k_EventName, k_MaxEventsPerHour,
20+
k_MaxNumberOfElements, k_VendorKey);
21+
#else // IF !UNITY_EDITOR
22+
AnalyticsResult result = Analytics.RegisterEvent(k_EventName, k_MaxEventsPerHour,
23+
k_MaxNumberOfElements, k_VendorKey);
24+
#endif
25+
26+
bool isResultOk = result == AnalyticsResult.Ok;
27+
return isResultOk;
28+
}
29+
30+
[Serializable]
31+
struct VspAttributionData
32+
{
33+
public string actionName;
34+
public string partnerName;
35+
public string customerUid;
36+
public string extra;
37+
}
38+
39+
/// <summary>
40+
/// Registers and attempts to send a VSP Attribution event.
41+
/// </summary>
42+
/// <param name="actionName">Name of the action, identifying a place this event was called from.</param>
43+
/// <param name="partnerName">Identifiable Verified Solutions Partner name.</param>
44+
/// <param name="customerUid">Unique identifier of the customer using Partner's Verified Solution.</param>
45+
public static AnalyticsResult SendAttributionEvent(string actionName, string partnerName, string customerUid)
46+
{
47+
try
48+
{
49+
#if UNITY_EDITOR
50+
// Are Editor Analytics enabled ? (Preferences)
51+
// The event shouldn't be able to report if this is disabled but if we know we're not going to report
52+
// lets early out and not spend time gathering all the data
53+
bool isEditorAnalyticsEnabled = EditorAnalytics.enabled;
54+
55+
if (!isEditorAnalyticsEnabled)
56+
return AnalyticsResult.AnalyticsDisabled;
57+
#else // IF !UNITY_EDITOR
58+
bool isRuntimeAnalyticsEnabled = Analytics.enabled;
59+
60+
if (!isRuntimeAnalyticsEnabled)
61+
return AnalyticsResult.AnalyticsDisabled;
62+
63+
if (!Debug.isDebugBuild)
64+
return AnalyticsResult.UnsupportedPlatform;
65+
#endif
66+
67+
// Can an event be registered?
68+
bool isEventRegistered = RegisterEvent();
69+
70+
if (!isEventRegistered)
71+
return AnalyticsResult.InvalidData;
72+
73+
// Create an expected data object
74+
var eventData = new VspAttributionData
75+
{
76+
actionName = actionName,
77+
partnerName = partnerName,
78+
customerUid = customerUid,
79+
extra = "{}"
80+
};
81+
82+
#if UNITY_EDITOR
83+
// Send the Attribution Event
84+
var eventResult = EditorAnalytics.SendEventWithLimit(k_EventName, eventData);
85+
#else // IF !UNITY_EDITOR
86+
var eventResult = Analytics.SendEvent(k_EventName, eventData);
87+
#endif
88+
return eventResult;
89+
}
90+
catch
91+
{
92+
// Fail silently
93+
return AnalyticsResult.AnalyticsDisabled;
94+
}
95+
}
96+
97+
#if ONE_SIGNAL_INSTALLED
98+
[RuntimeInitializeOnLoadMethod]
99+
public static void AttachToInit()
100+
=> OneSignal.OnInitialize += appId => SendAttributionEvent("Login", "OneSignal", appId);
101+
#endif
102+
}
103+
}

OneSignalExample/Assets/OneSignal/Attribution/VSPAttribution.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

OneSignalExample/Assets/OneSignal/Editor/Resources/OneSignalFileInventory.asset

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ MonoBehaviour:
1313
m_Name: OneSignalFileInventory
1414
m_EditorClassIdentifier:
1515
DistributedPaths:
16+
- Assets/OneSignal/Attribution.meta
1617
- Assets/OneSignal/CHANGELOG.md
1718
- Assets/OneSignal/CHANGELOG.md.meta
1819
- Assets/OneSignal/Editor.meta
@@ -23,6 +24,10 @@ MonoBehaviour:
2324
- Assets/OneSignal/README.md.meta
2425
- Assets/OneSignal/VERSION
2526
- Assets/OneSignal/VERSION.meta
27+
- Assets/OneSignal/Attribution/OneSignal.UnityPackage.Attribution.asmdef
28+
- Assets/OneSignal/Attribution/OneSignal.UnityPackage.Attribution.asmdef.meta
29+
- Assets/OneSignal/Attribution/VSPAttribution.cs
30+
- Assets/OneSignal/Attribution/VSPAttribution.cs.meta
2631
- Assets/OneSignal/Documentation~/asset_listing.png
2732
- Assets/OneSignal/Documentation~/menu_import_package.png
2833
- Assets/OneSignal/Documentation~/menu_import_package.png.meta
Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,46 @@
11
using System.IO;
2+
using System.Linq;
23
using UnityEditor;
3-
using UnityEditorInternal;
4-
using UnityEngine;
54

65
/// <summary>
76
/// Creates a unitypackage file for publishing
87
/// </summary>
9-
public static class OneSignalPackagePublisher
10-
{
11-
public static void UpdateProjectVersion()
12-
{
13-
var packageVersion = File.ReadAllText(_versionFilePath);
8+
public static class OneSignalPackagePublisher {
9+
public static void UpdateProjectVersion() {
10+
var packageVersion = File.ReadAllText(VersionFilePath);
1411
PlayerSettings.bundleVersion = packageVersion;
1512
}
1613

17-
public static void ExportUnityPackage()
18-
{
14+
[MenuItem("OneSignal/ExportUnityPackage")]
15+
public static void ExportUnityPackage() {
1916
AssetDatabase.Refresh();
20-
var packageVersion = File.ReadAllText(_versionFilePath);
17+
var packageVersion = File.ReadAllText(VersionFilePath);
2118
var packageName = $"OneSignal-v{packageVersion}.unitypackage";
2219

2320
AssetDatabase.ExportPackage(
24-
_filesPath,
21+
_filePaths(),
2522
packageName,
2623
ExportPackageOptions.Recurse | ExportPackageOptions.IncludeDependencies
2724
);
2825
}
2926

30-
private const string _versionFilePath = "Assets/OneSignal/VERSION";
31-
private const string _filesPath = "Assets/OneSignal";
27+
private static readonly string PackagePath = Path.Combine("Assets", "OneSignal");
28+
private static readonly string VersionFilePath = Path.Combine(PackagePath, "VERSION");
29+
30+
private static readonly string[] Exclusions = {
31+
Path.Combine(PackagePath, "Attribution"),
32+
".DS_Store"
33+
};
34+
35+
private static string[] _filePaths() {
36+
var files = Directory.GetFileSystemEntries(PackagePath);
37+
var pathsToInclude = files.Where(file => {
38+
if (file.EndsWith(".meta"))
39+
file = file.Substring(0, file.Length - 5);
40+
41+
return !Exclusions.Contains(file);
42+
});
43+
44+
return pathsToInclude.ToArray();
45+
}
3246
}

com.onesignal.unity.core/Runtime/AssemblyInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
[assembly: InternalsVisibleTo("OneSignal.iOS")]
44
[assembly: InternalsVisibleTo("OneSignal.Android")]
55
[assembly: InternalsVisibleTo("OneSignal.Core.Editor")]
6+
[assembly: InternalsVisibleTo("OneSignal.UnityPackage.Attribution")]

com.onesignal.unity.core/Runtime/Core/OneSignal.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ public UnityBuilder Settings(Dictionary<string, bool> settings)
383383
public void EndInit()
384384
{
385385
Init();
386+
OnInitialize?.Invoke(appID);
386387
}
387388

388389
/// <summary>
@@ -400,6 +401,8 @@ public UnityBuilder SetRequiresUserPrivacyConsent(bool required)
400401
}
401402
}
402403

404+
405+
internal static event Action<string> OnInitialize;
403406
internal static UnityBuilder builder = null;
404407

405408
/// <summary>

0 commit comments

Comments
 (0)