Skip to content

Commit 8fef662

Browse files
committed
feat: store The URL of the page the user landed on
1 parent 561f13f commit 8fef662

File tree

4 files changed

+48
-23
lines changed

4 files changed

+48
-23
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ If a visitor arrives at a website that uses the Nuxt UTM module and a UTM parame
2020

2121
- **📍 UTM Tracking**: Easily capture UTM parameters to gain insights into traffic sources and campaign performance.
2222
- **🔍 Intelligent De-duplication**: Smart recognition of page refreshes to avoid data duplication, ensuring each visit is uniquely accounted for.
23-
- **🔗 Comprehensive Data Collection**: Alongside UTM parameters, gather additional context such as referrer details, user agent, browser language, and screen resolution. This enriched data empowers your marketing strategies with a deeper understanding of campaign impact.
23+
- **🔗 Comprehensive Data Collection**: Alongside UTM parameters, gather additional context such as referrer details, user agent, landing page url, browser language, and screen resolution. This enriched data empowers your marketing strategies with a deeper understanding of campaign impact.
2424

2525
## Quick Setup
2626

@@ -73,6 +73,7 @@ The `$utm` will contain an array of UTM parameters collected for use. Each eleme
7373
referrer: "http://referrer.url", // Referrer URL
7474
userAgent: "User-Agent String", // User-Agent string of the browser
7575
language: "en-GB", // Language setting of the browser
76+
landingPageUrl: "http://landingpage.url", // The URL of the page the user landed on
7677
screen: {
7778
width: 1728,
7879
height: 1117,

src/runtime/utm.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export const getAdditionalInfo = (): AdditionalInfo => {
5454
referrer: document.referrer,
5555
userAgent: navigator.userAgent,
5656
language: navigator.language,
57+
landingPageUrl: window.location.href,
5758
screen: {
5859
width: screen.width,
5960
height: screen.height,

test/integration.test.ts

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DataObject } from 'nuxt-utm';
1+
import { DataObject } from "nuxt-utm";
22
import { describe, it, expect, beforeEach } from "vitest";
33
import { fileURLToPath } from "node:url";
44
import { setup, $fetch, createPage } from "@nuxt/test-utils";
@@ -10,33 +10,55 @@ describe("ssr", async () => {
1010
browser: true,
1111
});
1212

13+
let entries: DataObject[];
14+
let page: Page;
15+
16+
beforeEach(async () => {
17+
page = await createPage(
18+
"/?utm_source=test_source&utm_medium=test_medium&utm_campaign=test_campaign&utm_term=test_term&utm_content=test_content"
19+
);
20+
const rawData = await page.evaluate(() =>
21+
window.localStorage.getItem("nuxt-utm-data")
22+
);
23+
entries = await JSON.parse(rawData ?? "[]");
24+
});
25+
1326
describe("Module Playground", () => {
1427
it("Renders the index page", async () => {
1528
const html = await $fetch("/");
1629
expect(html).toContain("<h1>UTM Tracker</h1>");
1730
});
1831
});
1932

20-
describe("UTM params", () => {
21-
let utmParamsArray: DataObject[];
22-
let page: Page;
33+
describe("Additional info", () => {
34+
it("Stores data in local storage", () => {
35+
expect(entries?.[0]).toBeDefined();
36+
});
2337

24-
beforeEach(async () => {
25-
page = await createPage(
26-
"/?utm_source=test_source&utm_medium=test_medium&utm_campaign=test_campaign&utm_term=test_term&utm_content=test_content"
27-
);
28-
const rawData = await page.evaluate(() =>
29-
window.localStorage.getItem("nuxt-utm-data")
30-
);
31-
utmParamsArray = await JSON.parse(rawData ?? "[]");
38+
it("Stores Additional info", () => {
39+
expect(entries?.[0].additionalInfo).toBeDefined();
3240
});
3341

34-
it("Stores data in local storage", async () => {
35-
expect(utmParamsArray?.[0]).toBeDefined();
42+
it("Stores the correct values", async () => {
43+
const info = await page.evaluate(() => {
44+
return {
45+
referrer: document.referrer,
46+
userAgent: navigator.userAgent,
47+
language: navigator.language,
48+
landingPageUrl: window.location.href,
49+
screen: {
50+
width: screen.width,
51+
height: screen.height,
52+
},
53+
};
54+
});
55+
expect(entries?.[0].additionalInfo).toEqual(info);
3656
});
57+
});
3758

38-
it("Stores UTM params", async () => {
39-
expect(utmParamsArray?.[0].utmParams).toEqual({
59+
describe("UTM params", () => {
60+
it("Stores UTM params", () => {
61+
expect(entries?.[0].utmParams).toEqual({
4062
utm_campaign: "test_campaign",
4163
utm_content: "test_content",
4264
utm_medium: "test_medium",
@@ -50,8 +72,8 @@ describe("ssr", async () => {
5072
const rawData = await page?.evaluate(() =>
5173
window.localStorage.getItem("nuxt-utm-data")
5274
);
53-
utmParamsArray = await JSON.parse(rawData ?? "[]");
54-
expect(utmParamsArray.length).toEqual(1);
75+
entries = await JSON.parse(rawData ?? "[]");
76+
expect(entries.length).toEqual(1);
5577
});
5678

5779
it("Stores a new value if the UTM params are different but the session is the same", async () => {
@@ -62,8 +84,8 @@ describe("ssr", async () => {
6284
const rawData = await page.evaluate(() =>
6385
localStorage.getItem("nuxt-utm-data")
6486
);
65-
utmParamsArray = await JSON.parse(rawData ?? "[]");
66-
expect(utmParamsArray[0].utmParams).toEqual({
87+
entries = await JSON.parse(rawData ?? "[]");
88+
expect(entries[0].utmParams).toEqual({
6789
utm_campaign: "test_campaign2",
6890
utm_content: "test_content2",
6991
utm_medium: "test_medium2",
@@ -80,8 +102,8 @@ describe("ssr", async () => {
80102
const rawData = await page.evaluate(() =>
81103
localStorage.getItem("nuxt-utm-data")
82104
);
83-
utmParamsArray = await JSON.parse(rawData ?? "[]");
84-
expect(utmParamsArray[0].utmParams).toEqual({
105+
entries = await JSON.parse(rawData ?? "[]");
106+
expect(entries[0].utmParams).toEqual({
85107
utm_campaign: "test_campaign",
86108
utm_content: "test_content",
87109
utm_medium: "test_medium",

types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ declare module "nuxt-utm" {
1111
referrer: string;
1212
userAgent: string;
1313
language: string;
14+
landingPageUrl: string;
1415
screen: {
1516
width: number;
1617
height: number;

0 commit comments

Comments
 (0)