Skip to content

Commit

Permalink
Add optional VS attribution #STRINGS-925 (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
jablan authored Dec 2, 2024
1 parent d8bb5b4 commit 39ff18c
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 4 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# Changelog

## 1.0.0 - 2024-09-24
## 1.1.0 - 2024-11-28

Initial release
Add optional VS attribution

## 1.0.1 - 2024-10-22

Add compatibility for Unity 2019

## 1.0.0 - 2024-09-24

Initial release
2 changes: 1 addition & 1 deletion Editor/PhraseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected override async Task<HttpResponseMessage> SendAsync(

public class PhraseClient
{
private readonly string Version = "1.0.1";
private readonly string Version = "1.1.0";

private readonly PhraseProvider Provider;

Expand Down
9 changes: 9 additions & 0 deletions Editor/PhraseProvider.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Uncomment the following line when publishing the plugin to the Asset Store
//#define PHRASE_VS_ATTRIBUTION

using System.Collections;
using System.Collections.Generic;
using System.Data;
Expand All @@ -18,6 +21,9 @@
using Unity.EditorCoroutines.Editor;

using static Phrase.PhraseClient;
#if PHRASE_VS_ATTRIBUTION
using static Phrase.VSAttribution.VSAttribution;
#endif

namespace Phrase
{
Expand Down Expand Up @@ -540,6 +546,9 @@ private void ShowProjectSection()
phraseProvider.m_selectedProjectId = selectedProject.id;
phraseProvider.m_selectedAccountId = selectedProject.account.id;
phraseProvider.FetchLocales();
#if PHRASE_VS_ATTRIBUTION
SendAttributionEvent("ProjectAccess", "Phrase", phraseProvider.m_selectedAccountId);
#endif
}
}
}
Expand Down
98 changes: 98 additions & 0 deletions Editor/VSAttribution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using UnityEngine;
using UnityEditor;
using UnityEngine.Analytics;

namespace Phrase.VSAttribution
{
public static class VSAttribution
{
const int k_VersionId = 4;
const int k_MaxEventsPerHour = 10;
const int k_MaxNumberOfElements = 1000;

const string k_VendorKey = "unity.vsp-attribution";
const string k_EventName = "vspAttribution";

#if UNITY_2023_2_OR_NEWER
[AnalyticInfo(eventName: k_EventName, vendorKey: k_VendorKey, maxEventsPerHour: k_MaxEventsPerHour, maxNumberOfElements: k_MaxNumberOfElements, version: k_VersionId)]
private class VSAttributionAnalytic : IAnalytic
{
private VSAttributionData _data;

public VSAttributionAnalytic(VSAttributionData data)
{
_data = data;
}

public bool TryGatherData(out IAnalytic.IData data, out Exception error)
{
error = null;
data = _data;
return data != null;
}
}
#else
static bool RegisterEvent()
{
AnalyticsResult result = EditorAnalytics.RegisterEventWithLimit(k_EventName, k_MaxEventsPerHour,
k_MaxNumberOfElements, k_VendorKey, k_VersionId);

var isResultOk = result == AnalyticsResult.Ok;
return isResultOk;
}
#endif

[Serializable]
struct VSAttributionData
#if UNITY_2023_2_OR_NEWER
: IAnalytic.IData
#endif
{
public string actionName;
public string partnerName;
public string customerUid;
public string extra;
}

/// <summary>
/// Registers and attempts to send a Verified Solutions Attribution event.
/// </summary>
/// <param name="actionName">Name of the action, identifying a place this event was called from.</param>
/// <param name="partnerName">Identifiable Verified Solutions Partner's name.</param>
/// <param name="customerUid">Unique identifier of the customer using Partner's Verified Solution.</param>
public static AnalyticsResult SendAttributionEvent(string actionName, string partnerName, string customerUid)
{
try
{
// Are Editor Analytics enabled ? (Preferences)
if (!EditorAnalytics.enabled)
return AnalyticsResult.AnalyticsDisabled;

#if !UNITY_2023_2_OR_NEWER
if (!RegisterEvent())
return AnalyticsResult.InvalidData;
#endif
// Create an expected data object
var eventData = new VSAttributionData
{
actionName = actionName,
partnerName = partnerName,
customerUid = customerUid,
extra = "{}"
};
#if UNITY_2023_2_OR_NEWER
VSAttributionAnalytic analytic = new VSAttributionAnalytic(eventData);
return EditorAnalytics.SendAnalytic(analytic);
#else
return EditorAnalytics.SendEventWithLimit(k_EventName, eventData, k_VersionId);
#endif
}
catch
{
// Fail silently
return AnalyticsResult.AnalyticsDisabled;
}
}
}
}
11 changes: 11 additions & 0 deletions Editor/VSAttribution.cs.meta

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.phrase.plugin",
"version": "1.0.1",
"version": "1.1.0",
"displayName": "Phrase Localization Plugin",
"description": "This package provides synchronization with Phrase Strings localization service.",
"unity": "2019.1",
Expand Down

0 comments on commit 39ff18c

Please sign in to comment.