Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(metrics): add product_id and plan_id to more amplitude events
Made the Flow model follow Backbone conventions more closely where the first argument to the constructor is attributes and the second is options. More robust checking when fetching the product ID from the path. The plan_id is now optional. Combined effort of @philbooth and @shane-tomlinson, Phil did thet hard work. issue #989
- Loading branch information
1 parent
8716f77
commit ed501fa
Showing
20 changed files
with
412 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,4 +1,6 @@ | |||
{ | { | ||
"singleQuote": true, | "singleQuote": true, | ||
"trailingComma": "es5" | "trailingComma": "es5", | ||
"tabWidth": 2, | |||
"useTabs": false | |||
} | } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
packages/fxa-content-server/app/scripts/models/subscription.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,101 @@ | |||
/* This Source Code Form is subject to the terms of the Mozilla Public | |||
* License, v. 2.0. If a copy of the MPL was not distributed with this | |||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |||
|
|||
/** | |||
* A model to represent current subscription state, so that metrics can | |||
* associate events with products and plans. | |||
* | |||
* Tries to read data from the URL or, failing that, the resume token. | |||
*/ | |||
|
|||
import Backbone from 'backbone'; | |||
import Cocktail from '../lib/cocktail'; | |||
import ResumeTokenMixin from './mixins/resume-token'; | |||
import UrlMixin from './mixins/url'; | |||
|
|||
const SUBSCRIBE_PRODUCT_PATHNAME_REGEXP = /^\/subscriptions\/products\/(\w+)/; | |||
|
|||
// Neither UrlMixin nor ResumeTokenMixin are classes or interfaces | |||
interface IUrlMixin { | |||
getSearchParam(paramName: string): string | undefined; | |||
getPathname(): string; | |||
} | |||
|
|||
interface IResumeTokenMixin { | |||
populateFromStringifiedResumeToken(resumeToken: string); | |||
} | |||
|
|||
const RESUME_TOKEN_FIELDS = ['planId', 'productId']; | |||
class SubscriptionModel extends Backbone.Model { | |||
window?: Window; | |||
resumeTokenFields?: string[]; | |||
|
|||
constructor(attrs = {}, options) { | |||
super( | |||
{ | |||
planId: null, | |||
productId: null, | |||
...attrs, | |||
}, | |||
options | |||
); | |||
} | |||
|
|||
initialize(attrs = {}, options: { window?: Window } = {}) { | |||
if (this.get('planId') && this.get('productId')) { | |||
// already set, no need to look anywhere else for the values. | |||
return; | |||
} | |||
|
|||
this.window = options.window || window; | |||
this.resumeTokenFields = RESUME_TOKEN_FIELDS; | |||
|
|||
this._setSubscriptionInfoFromUrl(); | |||
if (this.get('productId')) { | |||
return; | |||
} | |||
|
|||
this._setSubscriptionInfoFromResumeToken(); | |||
} | |||
|
|||
_setSubscriptionInfoFromUrl() { | |||
const productId = this._getProductIdFromPathname(this.getPathname()); | |||
if (productId) { | |||
// If the user is browsing directly to /subscriptions/prod_* | |||
this.set('productId', productId); | |||
|
|||
// only get the planId from the query params if the user is at | |||
// a product path. It's possible for the plan to not be | |||
// specified, in which case the default plan will be used. | |||
const planId = this.getSearchParam('plan'); | |||
if (planId) { | |||
this.set('planId', planId); | |||
} | |||
} | |||
} | |||
|
|||
_getProductIdFromPathname(pathname: string) { | |||
const result = SUBSCRIBE_PRODUCT_PATHNAME_REGEXP.exec(pathname); | |||
if (result) { | |||
return result[1]; | |||
} | |||
return; | |||
} | |||
|
|||
_setSubscriptionInfoFromResumeToken() { | |||
const resumeToken = this.getSearchParam('resume'); | |||
if (resumeToken) { | |||
// this.resumeTokenFields is not set until the constructor | |||
// completes, and this method can be called from within | |||
// the constructor. Set the value | |||
this.populateFromStringifiedResumeToken(resumeToken); | |||
} | |||
} | |||
} | |||
|
|||
interface SubscriptionModel extends IUrlMixin, IResumeTokenMixin {} | |||
|
|||
Cocktail.mixin(SubscriptionModel, ResumeTokenMixin, UrlMixin); | |||
|
|||
export default SubscriptionModel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.