From 7754d3ca796f97453913cff812b129ca44dffbd8 Mon Sep 17 00:00:00 2001 From: Dimitri Mitropoulos Date: Mon, 10 Aug 2020 17:17:01 -0400 Subject: [PATCH 1/2] explicitly defines call patterns for page method --- lib/index.d.ts | 77 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 5 deletions(-) diff --git a/lib/index.d.ts b/lib/index.d.ts index d78c7a90..d0691f87 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -64,6 +64,10 @@ export declare namespace SegmentAnalytics { integrations?: SegmentIntegration; } + type PageProperties = Object; + type PageOptions = Object; + type PageCallback = () => void; + interface AnalyticsJS { Integrations: { [name: string]: unknown }; require: any; @@ -215,16 +219,79 @@ export declare namespace SegmentAnalytics { properties?: any ): AnalyticsJS; + /** - * Trigger a pageview, labeling the current page with an optional `category`, - * `name` and `properties`. + * Trigger a pageview. */ page( category: string, name: string, - properties: any, - options: any, - fn: unknown + properties: PageProperties, + options: PageOptions, + callback: PageCallback, + ): AnalyticsJS; + + /** + * Trigger a pageview. + */ + page( + category: string, + name: string, + properties: PageProperties, + callback: PageCallback, + ): AnalyticsJS; + + /** + * Trigger a pageview. + */ + page( + category: string, + name: string, + callback: PageCallback, + ): AnalyticsJS; + + /** + * Trigger a pageview. + */ + page( + name: string, + properties: PageProperties, + options: PageOptions, + callback: PageCallback, + ): AnalyticsJS; + + /** + * Trigger a pageview. + */ + page( + name: string, + properties: PageProperties, + callback: PageCallback, + ): AnalyticsJS; + + /** + * Trigger a pageview. + */ + page( + name: string, + callback: PageCallback, + ): AnalyticsJS; + + /** + * Trigger a pageview. + */ + page( + properties: PageProperties, + options: PageOptions, + callback: PageCallback, + ): AnalyticsJS; + + /** + * Trigger a pageview. + */ + page( + properties: PageProperties, + callback: PageCallback, ): AnalyticsJS; /** From 31b673a896d2bed33bc18137bd294593a4a90af4 Mon Sep 17 00:00:00 2001 From: Dimitri Mitropoulos Date: Tue, 11 Aug 2020 10:33:58 -0400 Subject: [PATCH 2/2] adds test and override for calling page with just name --- lib/index.d.ts | 7 +++++++ test/analytics.test.js | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/index.d.ts b/lib/index.d.ts index d0691f87..d2b9a111 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -277,6 +277,13 @@ export declare namespace SegmentAnalytics { callback: PageCallback, ): AnalyticsJS; + /** + * Trigger a pageview. + */ + page( + name: string, + ): AnalyticsJS; + /** * Trigger a pageview. */ diff --git a/test/analytics.test.js b/test/analytics.test.js index e42d90a5..7c539a56 100644 --- a/test/analytics.test.js +++ b/test/analytics.test.js @@ -730,6 +730,18 @@ describe('Analytics', function() { }); }); + it('should accept (name)', function() { + defaults.name = 'name'; + Test.prototype.page = sinon.spy(); + var test = new Test(); + test.invoke = sinon.spy(); + analytics.use(Test); + analytics.add(test); + analytics.initialize(); + analytics.page('Test Page Event'); + assert(test.invoke.called); + }); + it('should accept (properties, options, callback)', function(done) { analytics.page({}, {}, function() { var page = analytics._invoke.args[0][1];