posthog-node 3.2.1 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 3.3.0 - 2024-01-02
2
+
3
+ 1. Adds PostHogSentryIntegration to allow automatic capturing of exceptions reported via the @sentry/node package
4
+
1
5
  # 3.2.1 - 2023-12-15
2
6
 
3
7
  1. Fixes issue where a background refresh of feature flags could throw an unhandled error. It now emits to be detected by `.on('error', ...)`
package/index.ts CHANGED
@@ -1 +1,2 @@
1
1
  export * from './src/posthog-node'
2
+ export * from './src/extensions/sentry-integration'
package/lib/index.cjs.js CHANGED
@@ -158,7 +158,7 @@ function __spreadArray(to, from, pack) {
158
158
  return to.concat(ar || Array.prototype.slice.call(from));
159
159
  }
160
160
 
161
- var version = "3.2.1";
161
+ var version = "3.3.0";
162
162
 
163
163
  var PostHogPersistedProperty;
164
164
  (function (PostHogPersistedProperty) {
@@ -3009,5 +3009,95 @@ function (_super) {
3009
3009
  return PostHog;
3010
3010
  }(PostHogCoreStateless);
3011
3011
 
3012
+ /**
3013
+ * Integrate Sentry with PostHog. This will add a direct link to the person in Sentry, and an $exception event in PostHog.
3014
+ *
3015
+ * ### Usage
3016
+ *
3017
+ * Sentry.init({
3018
+ * dsn: 'https://example',
3019
+ * integrations: [
3020
+ * new PostHogSentryIntegration(posthog)
3021
+ * ]
3022
+ * })
3023
+ *
3024
+ * Sentry.setTag(PostHogSentryIntegration.POSTHOG_ID_TAG, 'some distinct id');
3025
+ *
3026
+ * @param {Object} [posthog] The posthog object
3027
+ * @param {string} [organization] Optional: The Sentry organization, used to send a direct link from PostHog to Sentry
3028
+ * @param {Number} [projectId] Optional: The Sentry project id, used to send a direct link from PostHog to Sentry
3029
+ * @param {string} [prefix] Optional: Url of a self-hosted sentry instance (default: https://sentry.io/organizations/)
3030
+ */
3031
+ var PostHogSentryIntegration =
3032
+ /** @class */
3033
+ function () {
3034
+ function PostHogSentryIntegration(posthog, posthogHost, organization, prefix) {
3035
+ var _a;
3036
+
3037
+ this.posthog = posthog;
3038
+ this.posthogHost = posthogHost;
3039
+ this.organization = organization;
3040
+ this.prefix = prefix;
3041
+ this.name = 'posthog-node';
3042
+ this.posthogHost = (_a = posthog.options.host) !== null && _a !== void 0 ? _a : 'https://app.posthog.com';
3043
+ }
3044
+
3045
+ PostHogSentryIntegration.prototype.setupOnce = function (addGlobalEventProcessor, getCurrentHub) {
3046
+ var _this = this;
3047
+
3048
+ addGlobalEventProcessor(function (event) {
3049
+ var _a, _b, _c, _d, _e, _f, _g, _h;
3050
+
3051
+ if (((_a = event.exception) === null || _a === void 0 ? void 0 : _a.values) === undefined || event.exception.values.length === 0) {
3052
+ return event;
3053
+ }
3054
+
3055
+ if (!event.tags) {
3056
+ event.tags = {};
3057
+ }
3058
+
3059
+ var sentry = getCurrentHub(); // Get the PostHog user ID from a specific tag, which users can set on their Sentry scope as they need.
3060
+
3061
+ var userId = event.tags[PostHogSentryIntegration.POSTHOG_ID_TAG];
3062
+
3063
+ if (userId === undefined) {
3064
+ // If we can't find a user ID, don't bother linking the event. We won't be able to send anything meaningful to PostHog without it.
3065
+ return event;
3066
+ }
3067
+
3068
+ event.tags['PostHog Person URL'] = new URL("/person/".concat(userId), _this.posthogHost).toString();
3069
+ var properties = {
3070
+ // PostHog Exception Properties
3071
+ $exception_message: (_b = event.exception.values[0]) === null || _b === void 0 ? void 0 : _b.value,
3072
+ $exception_type: (_c = event.exception.values[0]) === null || _c === void 0 ? void 0 : _c.type,
3073
+ $exception_personURL: event.tags['PostHog Person URL'],
3074
+ // Sentry Exception Properties
3075
+ $sentry_event_id: event.event_id,
3076
+ $sentry_exception: event.exception,
3077
+ $sentry_exception_message: (_d = event.exception.values[0]) === null || _d === void 0 ? void 0 : _d.value,
3078
+ $sentry_exception_type: (_e = event.exception.values[0]) === null || _e === void 0 ? void 0 : _e.type,
3079
+ $sentry_tags: event.tags
3080
+ };
3081
+ var projectId = (_g = (_f = sentry.getClient()) === null || _f === void 0 ? void 0 : _f.getDsn()) === null || _g === void 0 ? void 0 : _g.projectId;
3082
+
3083
+ if (_this.organization !== undefined && projectId !== undefined && event.event_id !== undefined) {
3084
+ properties.$sentry_url = "".concat((_h = _this.prefix) !== null && _h !== void 0 ? _h : 'https://sentry.io/organizations', "/").concat(_this.organization, "/issues/?project=").concat(projectId, "&query=").concat(event.event_id);
3085
+ }
3086
+
3087
+ _this.posthog.capture({
3088
+ event: '$exception',
3089
+ distinctId: userId,
3090
+ properties: properties
3091
+ });
3092
+
3093
+ return event;
3094
+ });
3095
+ };
3096
+
3097
+ PostHogSentryIntegration.POSTHOG_ID_TAG = 'posthog_distinct_id';
3098
+ return PostHogSentryIntegration;
3099
+ }();
3100
+
3012
3101
  exports.PostHog = PostHog;
3102
+ exports.PostHogSentryIntegration = PostHogSentryIntegration;
3013
3103
  //# sourceMappingURL=index.cjs.js.map