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/lib/index.d.ts CHANGED
@@ -316,7 +316,7 @@ declare class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
316
316
  private _memoryStorage;
317
317
  private featureFlagsPoller?;
318
318
  private maxCacheSize;
319
- private options;
319
+ readonly options: PostHogOptions;
320
320
  distinctIdHasSentFlagCalls: Record<string, string[]>;
321
321
  constructor(apiKey: string, options?: PostHogOptions);
322
322
  getPersistedProperty(key: PostHogPersistedProperty): any | undefined;
@@ -379,4 +379,44 @@ declare class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
379
379
  shutdownAsync(): Promise<void>;
380
380
  }
381
381
 
382
- export { PostHog, PostHogOptions };
382
+ /**
383
+ * @file Adapted from [posthog-js](https://github.com/PostHog/posthog-js/blob/8157df935a4d0e71d2fefef7127aa85ee51c82d1/src/extensions/sentry-integration.ts) with modifications for the Node SDK.
384
+ */
385
+
386
+ declare type _SentryEventProcessor = any;
387
+ declare type _SentryHub = any;
388
+ interface _SentryIntegration {
389
+ name: string;
390
+ setupOnce(addGlobalEventProcessor: (callback: _SentryEventProcessor) => void, getCurrentHub: () => _SentryHub): void;
391
+ }
392
+ /**
393
+ * Integrate Sentry with PostHog. This will add a direct link to the person in Sentry, and an $exception event in PostHog.
394
+ *
395
+ * ### Usage
396
+ *
397
+ * Sentry.init({
398
+ * dsn: 'https://example',
399
+ * integrations: [
400
+ * new PostHogSentryIntegration(posthog)
401
+ * ]
402
+ * })
403
+ *
404
+ * Sentry.setTag(PostHogSentryIntegration.POSTHOG_ID_TAG, 'some distinct id');
405
+ *
406
+ * @param {Object} [posthog] The posthog object
407
+ * @param {string} [organization] Optional: The Sentry organization, used to send a direct link from PostHog to Sentry
408
+ * @param {Number} [projectId] Optional: The Sentry project id, used to send a direct link from PostHog to Sentry
409
+ * @param {string} [prefix] Optional: Url of a self-hosted sentry instance (default: https://sentry.io/organizations/)
410
+ */
411
+ declare class PostHogSentryIntegration implements _SentryIntegration {
412
+ private readonly posthog;
413
+ private readonly posthogHost?;
414
+ private readonly organization?;
415
+ private readonly prefix?;
416
+ readonly name = "posthog-node";
417
+ static readonly POSTHOG_ID_TAG = "posthog_distinct_id";
418
+ constructor(posthog: PostHog, posthogHost?: string | undefined, organization?: string | undefined, prefix?: string | undefined);
419
+ setupOnce(addGlobalEventProcessor: (callback: _SentryEventProcessor) => void, getCurrentHub: () => _SentryHub): void;
420
+ }
421
+
422
+ export { PostHog, PostHogOptions, PostHogSentryIntegration };
package/lib/index.esm.js CHANGED
@@ -154,7 +154,7 @@ function __spreadArray(to, from, pack) {
154
154
  return to.concat(ar || Array.prototype.slice.call(from));
155
155
  }
156
156
 
157
- var version = "3.2.1";
157
+ var version = "3.3.0";
158
158
 
159
159
  var PostHogPersistedProperty;
160
160
  (function (PostHogPersistedProperty) {
@@ -3005,5 +3005,94 @@ function (_super) {
3005
3005
  return PostHog;
3006
3006
  }(PostHogCoreStateless);
3007
3007
 
3008
- export { PostHog };
3008
+ /**
3009
+ * Integrate Sentry with PostHog. This will add a direct link to the person in Sentry, and an $exception event in PostHog.
3010
+ *
3011
+ * ### Usage
3012
+ *
3013
+ * Sentry.init({
3014
+ * dsn: 'https://example',
3015
+ * integrations: [
3016
+ * new PostHogSentryIntegration(posthog)
3017
+ * ]
3018
+ * })
3019
+ *
3020
+ * Sentry.setTag(PostHogSentryIntegration.POSTHOG_ID_TAG, 'some distinct id');
3021
+ *
3022
+ * @param {Object} [posthog] The posthog object
3023
+ * @param {string} [organization] Optional: The Sentry organization, used to send a direct link from PostHog to Sentry
3024
+ * @param {Number} [projectId] Optional: The Sentry project id, used to send a direct link from PostHog to Sentry
3025
+ * @param {string} [prefix] Optional: Url of a self-hosted sentry instance (default: https://sentry.io/organizations/)
3026
+ */
3027
+ var PostHogSentryIntegration =
3028
+ /** @class */
3029
+ function () {
3030
+ function PostHogSentryIntegration(posthog, posthogHost, organization, prefix) {
3031
+ var _a;
3032
+
3033
+ this.posthog = posthog;
3034
+ this.posthogHost = posthogHost;
3035
+ this.organization = organization;
3036
+ this.prefix = prefix;
3037
+ this.name = 'posthog-node';
3038
+ this.posthogHost = (_a = posthog.options.host) !== null && _a !== void 0 ? _a : 'https://app.posthog.com';
3039
+ }
3040
+
3041
+ PostHogSentryIntegration.prototype.setupOnce = function (addGlobalEventProcessor, getCurrentHub) {
3042
+ var _this = this;
3043
+
3044
+ addGlobalEventProcessor(function (event) {
3045
+ var _a, _b, _c, _d, _e, _f, _g, _h;
3046
+
3047
+ if (((_a = event.exception) === null || _a === void 0 ? void 0 : _a.values) === undefined || event.exception.values.length === 0) {
3048
+ return event;
3049
+ }
3050
+
3051
+ if (!event.tags) {
3052
+ event.tags = {};
3053
+ }
3054
+
3055
+ var sentry = getCurrentHub(); // Get the PostHog user ID from a specific tag, which users can set on their Sentry scope as they need.
3056
+
3057
+ var userId = event.tags[PostHogSentryIntegration.POSTHOG_ID_TAG];
3058
+
3059
+ if (userId === undefined) {
3060
+ // 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.
3061
+ return event;
3062
+ }
3063
+
3064
+ event.tags['PostHog Person URL'] = new URL("/person/".concat(userId), _this.posthogHost).toString();
3065
+ var properties = {
3066
+ // PostHog Exception Properties
3067
+ $exception_message: (_b = event.exception.values[0]) === null || _b === void 0 ? void 0 : _b.value,
3068
+ $exception_type: (_c = event.exception.values[0]) === null || _c === void 0 ? void 0 : _c.type,
3069
+ $exception_personURL: event.tags['PostHog Person URL'],
3070
+ // Sentry Exception Properties
3071
+ $sentry_event_id: event.event_id,
3072
+ $sentry_exception: event.exception,
3073
+ $sentry_exception_message: (_d = event.exception.values[0]) === null || _d === void 0 ? void 0 : _d.value,
3074
+ $sentry_exception_type: (_e = event.exception.values[0]) === null || _e === void 0 ? void 0 : _e.type,
3075
+ $sentry_tags: event.tags
3076
+ };
3077
+ var projectId = (_g = (_f = sentry.getClient()) === null || _f === void 0 ? void 0 : _f.getDsn()) === null || _g === void 0 ? void 0 : _g.projectId;
3078
+
3079
+ if (_this.organization !== undefined && projectId !== undefined && event.event_id !== undefined) {
3080
+ 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);
3081
+ }
3082
+
3083
+ _this.posthog.capture({
3084
+ event: '$exception',
3085
+ distinctId: userId,
3086
+ properties: properties
3087
+ });
3088
+
3089
+ return event;
3090
+ });
3091
+ };
3092
+
3093
+ PostHogSentryIntegration.POSTHOG_ID_TAG = 'posthog_distinct_id';
3094
+ return PostHogSentryIntegration;
3095
+ }();
3096
+
3097
+ export { PostHog, PostHogSentryIntegration };
3009
3098
  //# sourceMappingURL=index.esm.js.map