-
Notifications
You must be signed in to change notification settings - Fork 0
Get getQueryString to expect relative URLs too #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
43e9be3
652d253
8faf51b
ef48d4d
52e2284
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -175,12 +175,35 @@ export class AnalyticsUrlParams { | |
| /** | ||
| * @return full query parameter string that can be appended to URLs | ||
| */ | ||
| getQueryString(destinationUrl?: URL, currentUrl?: URL): string { | ||
| getQueryString(destinationUrl?: URL | string, currentUrl?: URL): string { | ||
| // we first check if destionationUrl is a relative URL string. If it is, we exit. | ||
| const relativeRegex = /^(?!www\.|(?:http|ftp)s?:\/\/|[A-Za-z]:\\|\/\/).*/g; | ||
| if ( | ||
| typeof destinationUrl === 'string' && | ||
| destinationUrl.match(relativeRegex) | ||
| ) { | ||
| return ''; | ||
| } | ||
|
|
||
| if (typeof destinationUrl === 'string') { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can combine this if (typeof destinationUrl === 'string') {
if (destinationUrl.match(relativeRegex)) {
return '';
} else {
....
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gelbal Lol I was going to do it that way first, but for some reason I remembered that once I saw the if statements one below the other and that that was the usual practice or something like that and went the other way. Sure I'll change that.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @laziob oh I see you made a major rework. Actually I liked your previous implementation as it was cleaner (once you combine the new
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Petros (ex-CTO) made a pretty good presentation about such design principle actually (he calls it
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gelbal It still handles the string scenario first. The code above is for the other parameter that stays that way regardless of the Having said that, I could try go back to the previous design, but I could make it work with the previous design. This was so far the only way I achieved what I wanted lol.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To put an example,
So it explicitly uses the method on the only domain where its needed. For example marketing site only needs to use it on the Login and Signup buttons, which exist only in 2 places, the header (for most of the resin-site pages) , like here and the box for on the landing page. So its not that they need to detect relative urls in a simple way. Its that they have to detect URLs to another balena domain, not even including subdomains. If using a list is the best way to do it I dont really know, but if its needed to be raised for further discussion Im up for that. Whats the proper way and place to raise it? ALthough It just seems too much for the task at hand tbh.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gelbal we need to work on this soon since its the only thing preventing marketing sites to update to the last analytics client. Its still using 0.12 so its not stiching sessions nor using the last referrer updates.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @iamsolankiamit @thgreasi @JSReds if you could take a look into the discussion too so we can decide on how we advance it would be really helpful and appreciated! thanks!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey @laziob cc @thgreasi @JSReds @iamsolankiamit, how about introducing an optional parameter (in the We could retrieve this list either directly from the client that calls We will probably end up updating this list seldom so it's not a big maintenance issue to store it on the Edit: Thinking it further, part of my suggestion actually means that
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having said these, let's still go ahead with working on this PR to add support for relative URL detection. |
||
| try { | ||
| destinationUrl = new URL(destinationUrl); | ||
| } catch (err) { | ||
| console.error(err); | ||
| return ''; | ||
| } | ||
| } | ||
|
|
||
| // this regex is based on the assumption that we wont be using TLDs longer than 3 characters. If we do, it will break | ||
| // the logic and take that longer TLD as the main domain, for example hub.balena.edge.io -> edge.io | ||
| const regex = /([a-zA-Z0-9-]+)(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]+$)/g; | ||
|
|
||
| let actualDomainMatch; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually prefer it here, next to where it's first assigned, so that's easier to follow. |
||
| const destinationDomainMatch = destinationUrl | ||
| ? destinationUrl.hostname.match(regex) | ||
| : null; | ||
| const destinationDomain = destinationDomainMatch?.[0]; | ||
|
|
||
| let actualDomainMatch: RegExpMatchArray | null; | ||
| if (currentUrl) { | ||
| actualDomainMatch = currentUrl.hostname.match(regex); | ||
| } else if (typeof window !== 'undefined') { | ||
|
|
@@ -189,16 +212,7 @@ export class AnalyticsUrlParams { | |
| actualDomainMatch = null; | ||
| } | ||
|
|
||
| const destinationDomainMatch = destinationUrl | ||
| ? destinationUrl.hostname.match(regex) | ||
| : null; | ||
|
|
||
| const actualDomain = actualDomainMatch | ||
| ? actualDomainMatch.toString() | ||
| : null; | ||
| const destinationDomain = destinationDomainMatch | ||
| ? destinationDomainMatch.toString() | ||
| : null; | ||
| const actualDomain = actualDomainMatch?.[0]; | ||
|
|
||
| if (!destinationDomain || actualDomain !== destinationDomain) { | ||
| return [this.getDeviceIdsQueryString(), this.getSessionIdQueryString()] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "lib": [ | ||
| "ES6","DOM" | ||
| ], | ||
| "module": "commonjs", | ||
| "target": "es5", | ||
| "outDir": "dist", | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.