Skip to content

Commit 30e8e9e

Browse files
committed
chore: update github actions and add missing integration tests
1 parent f2e8b9a commit 30e8e9e

File tree

5 files changed

+72
-17
lines changed

5 files changed

+72
-17
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,14 @@ jobs:
3737
continue-on-error: true
3838
run: yarn list
3939

40-
# - name: Install dependencies in playground
41-
# run: yarn install
42-
# working-directory: ./playground
43-
- run: yarn install
40+
- name: Install dependencies
41+
run: yarn install
4442

4543
- name: Install Playwright Browsers
4644
run: npx playwright install --with-deps
4745

4846
- name: Build Playground
4947
run: yarn dev:build
5048

51-
52-
# - name: Build the playground
53-
# run: yarn build
54-
# working-directory: ./playground
55-
5649
- name: Execute tests
5750
run: yarn test

src/runtime/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default defineNuxtPlugin((nuxtApp) => {
4141
};
4242

4343
// Exit if the last entry is the same as the new entry
44-
if (isRepeatedEntry(data, sessionId)) return;
44+
if (isRepeatedEntry(data, dataObject)) return;
4545

4646
// Add the new item to the data array
4747
data.value.unshift(dataObject);

src/runtime/utm.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,19 @@ export const getAdditionalInfo = (): AdditionalInfo => {
6363

6464
export const isRepeatedEntry = (
6565
data: Ref<DataObject[]>,
66-
currentSessionID: string
66+
currentEntry: DataObject
6767
): boolean => {
6868
const lastEntry = data.value?.[0];
69-
return lastEntry && lastEntry.sessionId === currentSessionID;
69+
const lastUtm = lastEntry?.utmParams;
70+
const newUtm = currentEntry.utmParams;
71+
72+
return (
73+
lastEntry &&
74+
lastUtm.utm_campaign === newUtm.utm_campaign &&
75+
lastUtm.utm_content === newUtm.utm_content &&
76+
lastUtm.utm_medium === newUtm.utm_medium &&
77+
lastUtm.utm_source === newUtm.utm_source &&
78+
lastUtm.utm_term === newUtm.utm_term &&
79+
lastEntry.sessionId === currentEntry.sessionId
80+
);
7081
};

test/integration.test.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DataObject } from 'nuxt-utm';
12
import { describe, it, expect, beforeEach } from "vitest";
23
import { fileURLToPath } from "node:url";
34
import { setup, $fetch, createPage } from "@nuxt/test-utils";
@@ -17,8 +18,9 @@ describe("ssr", async () => {
1718
});
1819

1920
describe("UTM params", () => {
20-
let utmParamsArray: any[];
21+
let utmParamsArray: DataObject[];
2122
let page: Page;
23+
2224
beforeEach(async () => {
2325
page = await createPage(
2426
"/?utm_source=test_source&utm_medium=test_medium&utm_campaign=test_campaign&utm_term=test_term&utm_content=test_content"
@@ -32,6 +34,7 @@ describe("ssr", async () => {
3234
it("Stores data in local storage", async () => {
3335
expect(utmParamsArray?.[0]).toBeDefined();
3436
});
37+
3538
it("Stores UTM params", async () => {
3639
expect(utmParamsArray?.[0].utmParams).toEqual({
3740
utm_campaign: "test_campaign",
@@ -41,5 +44,50 @@ describe("ssr", async () => {
4144
utm_term: "test_term",
4245
});
4346
});
47+
48+
it("Doesn't store anything after a page reload with the same UTM params and session", async () => {
49+
await page.reload();
50+
const rawData = await page?.evaluate(() =>
51+
window.localStorage.getItem("nuxt-utm-data")
52+
);
53+
utmParamsArray = await JSON.parse(rawData ?? "[]");
54+
expect(utmParamsArray.length).toEqual(1);
55+
});
56+
57+
it("Stores a new value if the UTM params are different but the session is the same", async () => {
58+
const urlBase = page.url().split("?")[0];
59+
await page.goto(
60+
`${urlBase}/?utm_source=test_source2&utm_medium=test_medium2&utm_campaign=test_campaign2&utm_term=test_term2&utm_content=test_content2`
61+
);
62+
const rawData = await page.evaluate(() =>
63+
localStorage.getItem("nuxt-utm-data")
64+
);
65+
utmParamsArray = await JSON.parse(rawData ?? "[]");
66+
expect(utmParamsArray[0].utmParams).toEqual({
67+
utm_campaign: "test_campaign2",
68+
utm_content: "test_content2",
69+
utm_medium: "test_medium2",
70+
utm_source: "test_source2",
71+
utm_term: "test_term2",
72+
});
73+
});
74+
75+
it("Stores a new value if the UTM params are the same but the session is different", async () => {
76+
await page.evaluate(() =>
77+
sessionStorage.setItem("nuxt-utm-session-id", "old-session")
78+
);
79+
await page.reload();
80+
const rawData = await page.evaluate(() =>
81+
localStorage.getItem("nuxt-utm-data")
82+
);
83+
utmParamsArray = await JSON.parse(rawData ?? "[]");
84+
expect(utmParamsArray[0].utmParams).toEqual({
85+
utm_campaign: "test_campaign",
86+
utm_content: "test_content",
87+
utm_medium: "test_medium",
88+
utm_source: "test_source",
89+
utm_term: "test_term",
90+
});
91+
});
4492
});
4593
});

test/unit.test.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,22 @@ describe("getUtmParams function", () => {
7272

7373
describe("isRepeatedEntry function", () => {
7474
let data: Ref<DataObject[]>;
75+
let newUtmItem: DataObject;
7576
beforeEach(() => {
7677
data = ref<DataObject[]>(JSON.parse(`[${utmItem}]`));
78+
newUtmItem = JSON.parse(utmItem);
7779
});
7880

7981
it("Returns true if the utm params and the session already exists in local storage", () => {
80-
expect(isRepeatedEntry(data, "beai1gx7dg")).toBeTruthy();
82+
expect(isRepeatedEntry(data, newUtmItem)).toBeTruthy();
8183
});
8284
it("Returns false if the utm params are the same but the session is different", () => {
83-
expect(isRepeatedEntry(data, "newSession")).toBeFalsy();
85+
newUtmItem.sessionId = "New Session";
86+
expect(isRepeatedEntry(data, newUtmItem)).toBeFalsy();
8487
});
8588
it("Returns false if the utm params are different but the session is the same", () => {
86-
const data = ref<DataObject[]>(JSON.parse(`[${utmItem}]`));
87-
expect(isRepeatedEntry(data, "newSession")).toBeFalsy();
89+
newUtmItem.utmParams.utm_content = "New UTM content"
90+
expect(isRepeatedEntry(data, newUtmItem)).toBeFalsy();
8891
});
8992
});
9093

0 commit comments

Comments
 (0)