-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for tracking AD campaigns (#501)
- Loading branch information
1 parent
706ff13
commit 3ffc277
Showing
10 changed files
with
206 additions
and
12 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Column } from 'typeorm'; | ||
|
||
export default class TrackingEntity { | ||
@Column({ nullable: true, type: 'character varying' }) | ||
public campaignId: string | null; | ||
@Column({ nullable: true, type: 'character varying' }) | ||
public creativeId: string | null; | ||
@Column({ nullable: true, type: 'character varying' }) | ||
public device: string | null; | ||
@Column({ nullable: true, type: 'character varying' }) | ||
public keyword: string | null; | ||
@Column({ nullable: true, type: 'character varying' }) | ||
public gclid: string | null; | ||
|
||
constructor() { | ||
this.campaignId = null; | ||
this.creativeId = null; | ||
this.device = null; | ||
this.keyword = null; | ||
this.gclid = null; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
||
export class tracking1678985862753 implements MigrationInterface { | ||
name = 'tracking1678985862753' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "users" ADD "tracking_campaign_id" character varying`); | ||
await queryRunner.query(`ALTER TABLE "users" ADD "tracking_creative_id" character varying`); | ||
await queryRunner.query(`ALTER TABLE "users" ADD "tracking_device" character varying`); | ||
await queryRunner.query(`ALTER TABLE "users" ADD "tracking_keyword" character varying`); | ||
await queryRunner.query(`ALTER TABLE "users" ADD "tracking_gclid" character varying`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "users" DROP COLUMN "tracking_gclid"`); | ||
await queryRunner.query(`ALTER TABLE "users" DROP COLUMN "tracking_keyword"`); | ||
await queryRunner.query(`ALTER TABLE "users" DROP COLUMN "tracking_device"`); | ||
await queryRunner.query(`ALTER TABLE "users" DROP COLUMN "tracking_creative_id"`); | ||
await queryRunner.query(`ALTER TABLE "users" DROP COLUMN "tracking_campaign_id"`); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -791,6 +791,13 @@ | |
dependencies: | ||
"@types/node" "*" | ||
|
||
"@types/cookie-parser@^1.4.3": | ||
version "1.4.3" | ||
resolved "https://registry.yarnpkg.com/@types/cookie-parser/-/cookie-parser-1.4.3.tgz#3a01df117c5705cf89a84c876b50c5a1fd427a21" | ||
integrity sha512-CqSKwFwefj4PzZ5n/iwad/bow2hTCh0FlNAeWLtQM3JA/NX/iYagIpWG2cf1bQKQ2c9gU2log5VUCrn7LDOs0w== | ||
dependencies: | ||
"@types/express" "*" | ||
|
||
"@types/cookie@^0.4.1": | ||
version "0.4.1" | ||
resolved "https://registry.yarnpkg.com/@types/cookie/-/cookie-0.4.1.tgz#bfd02c1f2224567676c1545199f87c3a861d878d" | ||
|
@@ -1729,6 +1736,14 @@ convert-source-map@^2.0.0: | |
resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-2.0.0.tgz#4b560f649fc4e918dd0ab75cf4961e8bc882d82a" | ||
integrity sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg== | ||
|
||
cookie-parser@^1.4.6: | ||
version "1.4.6" | ||
resolved "https://registry.yarnpkg.com/cookie-parser/-/cookie-parser-1.4.6.tgz#3ac3a7d35a7a03bbc7e365073a26074824214594" | ||
integrity sha512-z3IzaNjdwUC2olLIB5/ITd0/setiaFMLYiZJle7xg5Fe9KWAceil7xszYfHHBtDFYLSgJduS2Ty0P1uJdPDJeA== | ||
dependencies: | ||
cookie "0.4.1" | ||
cookie-signature "1.0.6" | ||
|
||
[email protected]: | ||
version "1.0.6" | ||
resolved "https://registry.yarnpkg.com/cookie-signature/-/cookie-signature-1.0.6.tgz#e303a882b342cc3ee8ca513a79999734dab3ae2c" | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { addYears } from 'date-fns'; | ||
import { ResponseCookie } from 'next/dist/server/web/spec-extension/cookies'; | ||
import { NextResponse, NextRequest } from 'next/server'; | ||
import parse from 'url-parse'; | ||
|
||
// https://www.retrospected.com/?campaignid=19686942887&creative=648178043912&device=c&keyword=retro | ||
|
||
const COOKIE_NAME = 'retro-aw-tracking'; | ||
|
||
export type TrackingInfo = { | ||
campaignId: string; | ||
creativeId: string; | ||
device: string; | ||
keyword: string; | ||
gclid: string; | ||
}; | ||
|
||
export function middleware(request: NextRequest) { | ||
const response = NextResponse.next(); | ||
|
||
const url = parse(request.url, true); | ||
|
||
if (url.query && url.query.campaignid && url.query.creative) { | ||
const host = url.host; | ||
const tracking: Partial<TrackingInfo> = { | ||
campaignId: url.query.campaignid, | ||
creativeId: url.query.creative, | ||
device: url.query.device, | ||
keyword: url.query.keyword, | ||
gclid: url.query.gclid, | ||
}; | ||
|
||
const cookie: ResponseCookie = { | ||
name: COOKIE_NAME, | ||
value: JSON.stringify(tracking), | ||
sameSite: 'lax', | ||
expires: addYears(new Date(), 1), | ||
domain: | ||
host.includes('localhost') || host.split('.').length < 3 | ||
? undefined | ||
: host.split('.').slice(1).join('.'), | ||
}; | ||
|
||
console.log('tracking', tracking); | ||
|
||
response.cookies.set(cookie); | ||
} | ||
|
||
return response; | ||
} |
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