Skip to content

Commit ead00ab

Browse files
authored
Merge pull request #106 from arup-group/task/OGH-24-Add-PostHog-component
OGH-24-Add-PostHog-component
2 parents 0c26e83 + 8201679 commit ead00ab

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing;
4+
using Grasshopper.Kernel;
5+
using Newtonsoft.Json;
6+
using OasysGH;
7+
using OasysGH.Components;
8+
using OasysGH.Helpers;
9+
10+
namespace GH_UnitNumber.Components {
11+
public class SendToPostHog : GH_OasysComponent {
12+
public override Guid ComponentGuid => new Guid("9da7ba33-62fc-4f36-81a3-7e24a09a644b");
13+
public override GH_Exposure Exposure => GH_Exposure.hidden;
14+
public override OasysPluginInfo PluginInfo => GH_UnitNumberPluginInfo.Instance;
15+
protected override Bitmap Icon => null;
16+
17+
public SendToPostHog() : base("Send to PostHog", "PostHog",
18+
"Send an event to a PostHog server", "Params", "Util") {
19+
Hidden = true;
20+
}
21+
22+
protected override void RegisterInputParams(GH_InputParamManager pManager) {
23+
pManager.AddTextParameter("API key", "K", "PostHog API key", GH_ParamAccess.item);
24+
pManager.AddTextParameter("Event name", "E", "PostHog event name", GH_ParamAccess.item);
25+
pManager.AddTextParameter("Event properties", "P", "[Optional] PostHog event properties as JSON string", GH_ParamAccess.item);
26+
pManager.AddBooleanParameter("Enabled", "On", "[Optional] If true, the component will try to send the PostHog event", GH_ParamAccess.item, true);
27+
pManager[2].Optional = true;
28+
pManager[3].Optional = true;
29+
}
30+
31+
protected override void RegisterOutputParams(GH_OutputParamManager pManager) {
32+
}
33+
34+
protected override void SolveInstance(IGH_DataAccess da) {
35+
string apiKey = string.Empty;
36+
da.GetData(0, ref apiKey);
37+
38+
string eventName = string.Empty;
39+
da.GetData(1, ref eventName);
40+
41+
string json = string.Empty;
42+
da.GetData(2, ref json);
43+
44+
bool enabled = true;
45+
da.GetData(3, ref enabled);
46+
47+
if (enabled) {
48+
IDictionary<string, object> properties = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
49+
50+
_ = PostHog.SendToPostHog(apiKey, GH_UnitNumberPluginInfo.Instance.PluginName, GH_UnitNumberPluginInfo.Instance.Version, GH_UnitNumberPluginInfo.Instance.IsBeta, eventName, properties);
51+
}
52+
}
53+
}
54+
}

OasysGH/Helpers/PostHog.cs

+15-7
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ private class PhContainer {
1919
private string _event;
2020

2121
[JsonProperty("properties")]
22-
private Dictionary<string, object> _properties;
22+
private IDictionary<string, object> _properties;
2323

2424
[JsonProperty("timestamp")]
2525
private DateTime _timestamp;
2626

27-
public PhContainer(string apiKey, string eventName, Dictionary<string, object> properties) {
27+
public PhContainer(string apiKey, string eventName, IDictionary<string, object> properties) {
2828
_apiKey = apiKey;
2929
_event = eventName;
3030
_properties = properties;
@@ -84,16 +84,20 @@ public static void RemovedFromDocument(GH_Component component, OasysPluginInfo p
8484
}
8585
}
8686

87-
public static async Task<HttpResponseMessage> SendToPostHog(OasysPluginInfo pluginInfo, string eventName, Dictionary<string, object> additionalProperties = null) {
87+
public static async Task<HttpResponseMessage> SendToPostHog(OasysPluginInfo pluginInfo, string eventName, IDictionary<string, object> additionalProperties = null) {
88+
return await SendToPostHog(pluginInfo.PostHogApiKey, pluginInfo.PluginName, pluginInfo.Version, pluginInfo.IsBeta, eventName, additionalProperties);
89+
}
90+
91+
public static async Task<HttpResponseMessage> SendToPostHog(string postHogApiKey, string pluginName, string version, bool isBeta, string eventName, IDictionary<string, object> additionalProperties = null) {
8892
// posthog ADS plugin requires a user object
8993
User user = currentUser;
9094

9195
var properties = new Dictionary<string, object>() {
9296
{ "distinct_id", user.UserName },
9397
{ "user", user },
94-
{ "pluginName", pluginInfo.PluginName },
95-
{ "version", pluginInfo.Version },
96-
{ "isBeta", pluginInfo.IsBeta },
98+
{ "pluginName", pluginName },
99+
{ "version", version },
100+
{ "isBeta", isBeta },
97101
};
98102

99103
if (additionalProperties != null) {
@@ -102,7 +106,11 @@ public static async Task<HttpResponseMessage> SendToPostHog(OasysPluginInfo plug
102106
}
103107
}
104108

105-
var container = new PhContainer(pluginInfo.PostHogApiKey, eventName, properties);
109+
return await SendToPostHog(postHogApiKey, eventName, properties);
110+
}
111+
112+
public static async Task<HttpResponseMessage> SendToPostHog(string postHogApiKey, string eventName, IDictionary<string, object> properties) {
113+
var container = new PhContainer(postHogApiKey, eventName, properties);
106114
string body = JsonConvert.SerializeObject(container);
107115
var content = new StringContent(body, Encoding.UTF8, "application/json");
108116
HttpResponseMessage response = await phClient.PostAsync("https://eu.posthog.com/capture/", content);

0 commit comments

Comments
 (0)