-
Notifications
You must be signed in to change notification settings - Fork 49
Fix affiliate tracking URL origin #136
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
Closed
absalonCRC
wants to merge
2
commits into
profullstack:master
from
absalonCRC:fix-affiliate-tracking-url-origin
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import { NextRequest } from "next/server"; | ||
| import { GET } from "./route"; | ||
|
|
||
| function makeParams(code: string) { | ||
| return { params: Promise.resolve({ code }) }; | ||
| } | ||
|
|
||
| describe("GET /ref/[code]", () => { | ||
| afterEach(() => { | ||
| vi.unstubAllEnvs(); | ||
| }); | ||
|
|
||
| it("redirects to the current request origin when no app URL is configured", async () => { | ||
| vi.stubEnv("NEXT_PUBLIC_APP_URL", ""); | ||
|
|
||
| const res = await GET( | ||
| new NextRequest("http://localhost:3000/ref/alice code/1"), | ||
| makeParams("alice code/1") | ||
| ); | ||
|
|
||
| expect(res.status).toBe(307); | ||
| expect(res.headers.get("location")).toBe( | ||
| "http://localhost:3000/api/affiliates/click?ugig_ref=alice+code%2F1" | ||
| ); | ||
| }); | ||
|
|
||
| it("redirects to the configured public app URL when present", async () => { | ||
| vi.stubEnv("NEXT_PUBLIC_APP_URL", "https://staging.ugig.example"); | ||
|
|
||
| const res = await GET( | ||
| new NextRequest("http://localhost:3000/ref/alice"), | ||
| makeParams("alice") | ||
| ); | ||
|
|
||
| expect(res.headers.get("location")).toBe( | ||
| "https://staging.ugig.example/api/affiliates/click?ugig_ref=alice" | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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 | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import { buildAffiliateTrackingUrl, getAffiliateBaseUrl } from "./tracking-url"; | ||
|
|
||
| describe("affiliate tracking URLs", () => { | ||
| afterEach(() => { | ||
| vi.unstubAllEnvs(); | ||
| }); | ||
|
|
||
| it("uses the current request origin when no app URL is configured", () => { | ||
| vi.stubEnv("NEXT_PUBLIC_APP_URL", ""); | ||
|
|
||
| expect(getAffiliateBaseUrl("http://localhost:3000/api/affiliates/offers/offer-1/affiliates")) | ||
| .toBe("http://localhost:3000"); | ||
| }); | ||
|
|
||
| it("prefers the configured public app URL", () => { | ||
| vi.stubEnv("NEXT_PUBLIC_APP_URL", "https://staging.ugig.example"); | ||
|
|
||
| expect(getAffiliateBaseUrl("http://localhost:3000/api/affiliates/offers/offer-1/affiliates")) | ||
| .toBe("https://staging.ugig.example"); | ||
| }); | ||
|
|
||
| it("builds encoded click URLs", () => { | ||
| expect(buildAffiliateTrackingUrl("https://staging.ugig.example", "alice code/1")) | ||
| .toBe("https://staging.ugig.example/api/affiliates/click?ugig_ref=alice+code%2F1"); | ||
| }); | ||
| }); |
This file contains hidden or 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 | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| export function getAffiliateBaseUrl(requestUrl: string): string { | ||
| return process.env.NEXT_PUBLIC_APP_URL || new URL(requestUrl).origin; | ||
| } | ||
|
|
||
| export function buildAffiliateTrackingUrl(baseUrl: string, trackingCode: string): string { | ||
| const url = new URL("/api/affiliates/click", baseUrl); | ||
| url.searchParams.set("ugig_ref", trackingCode); | ||
| return url.toString(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tracking_urlformat changed from short/ref/link to raw click endpointThe old response returned
…/ref/{trackingCode}— the purpose-built short link that affiliates share with customers. This PR changes it to…/api/affiliates/click?ugig_ref={trackingCode}, which is the internal click-tracking handler that/ref/[code]redirects to. Any client or affiliate dashboard that stores or displays thetracking_urlfrom the apply response will now see and distribute the raw API URL instead of the clean shareable link. Both resolve to the same destination, but this is a breaking change to the response contract that isn't covered by a test forapply/route.ts.