2023-06-21 11:56:59 +03:00
|
|
|
const assert = require('assert/strict');
|
2022-10-10 18:15:31 +03:00
|
|
|
const fetch = require('node-fetch').default;
|
|
|
|
const {agentProvider, mockManager, fixtureManager} = require('../utils/e2e-framework');
|
|
|
|
const urlUtils = require('../../core/shared/url-utils');
|
2023-03-07 18:08:40 +03:00
|
|
|
const jobService = require('../../core/server/services/jobs/job-service');
|
2022-10-10 18:15:31 +03:00
|
|
|
|
|
|
|
describe('Click Tracking', function () {
|
|
|
|
let agent;
|
|
|
|
|
|
|
|
before(async function () {
|
2023-03-07 18:08:40 +03:00
|
|
|
const {adminAgent} = await agentProvider.getAgentsWithFrontend();
|
|
|
|
agent = adminAgent;
|
2022-10-10 18:15:31 +03:00
|
|
|
await fixtureManager.init('newsletters', 'members:newsletters');
|
|
|
|
await agent.loginAsOwner();
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
mockManager.mockMail();
|
2023-03-07 18:08:40 +03:00
|
|
|
mockManager.mockMailgun();
|
2022-10-10 18:15:31 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
mockManager.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Full test', async function () {
|
2024-03-26 19:51:23 +03:00
|
|
|
const siteUrl = new URL(urlUtils.urlFor('home', true));
|
|
|
|
|
|
|
|
const {body: {posts: [draft]}} = await agent.post('/posts/?source=html', {
|
2022-10-10 18:15:31 +03:00
|
|
|
body: {
|
|
|
|
posts: [{
|
2024-03-26 19:51:23 +03:00
|
|
|
title: 'My Newsletter',
|
|
|
|
html: `<p>External link <a href="https://example.com/a">https://example.com/a</a>; Internal link <a href=${siteUrl.href}/about">${siteUrl.href}/about</a>;Ghost homepage <a href="https://ghost.org">https://ghost.org</a></p>`
|
2022-10-10 18:15:31 +03:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const newsletterSlug = fixtureManager.get('newsletters', 0).slug;
|
|
|
|
const {body: {posts: [post]}} = await agent.put(
|
|
|
|
`/posts/${draft.id}/?newsletter=${newsletterSlug}`,
|
|
|
|
{
|
|
|
|
body: {
|
|
|
|
posts: [{
|
|
|
|
updated_at: draft.updated_at,
|
|
|
|
status: 'published'
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2023-03-07 18:08:40 +03:00
|
|
|
// Wait for the newsletter to be sent
|
|
|
|
await jobService.allSettled();
|
|
|
|
|
2022-10-10 18:15:31 +03:00
|
|
|
const {body: {links}} = await agent.get(
|
2023-11-21 11:45:36 +03:00
|
|
|
`/links/?filter=${encodeURIComponent(`post_id:'${post.id}'`)}`
|
2022-10-10 18:15:31 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
/** @type {(url: string) => Promise<import('node-fetch').Response>} */
|
|
|
|
const fetchWithoutFollowingRedirect = url => fetch(url, {redirect: 'manual'});
|
|
|
|
|
|
|
|
let internalRedirectHappened = false;
|
|
|
|
let externalRedirectHappened = false;
|
2024-03-26 19:51:23 +03:00
|
|
|
let poweredByGhostIgnored = true;
|
|
|
|
|
2022-10-10 18:15:31 +03:00
|
|
|
for (const link of links) {
|
|
|
|
const res = await fetchWithoutFollowingRedirect(link.link.from);
|
|
|
|
const redirectedToUrl = new URL(res.headers.get('location'));
|
|
|
|
|
|
|
|
// startsWith is a little dirty, but we need this because siteUrl
|
|
|
|
// can have a path when Ghost is hosted on a subdomain.
|
|
|
|
const isInternal = redirectedToUrl.href.startsWith(siteUrl.href);
|
|
|
|
if (isInternal) {
|
|
|
|
internalRedirectHappened = true;
|
|
|
|
|
|
|
|
assert(redirectedToUrl.searchParams.get('attribution_id'), 'attribution_id should be present on internal redirects');
|
|
|
|
assert(redirectedToUrl.searchParams.get('attribution_type'), 'attribution_type should be present on internal redirects');
|
|
|
|
} else {
|
|
|
|
externalRedirectHappened = true;
|
|
|
|
|
|
|
|
assert(!redirectedToUrl.searchParams.get('attribution_id'), 'attribution_id should not be present on internal redirects');
|
|
|
|
assert(!redirectedToUrl.searchParams.get('attribution_type'), 'attribution_type should not be present on internal redirects');
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(redirectedToUrl.searchParams.get('ref'), 'ref should be present on all redirects');
|
2024-03-26 19:51:23 +03:00
|
|
|
|
|
|
|
// Powered by Ghost link should not be replaced / tracked
|
|
|
|
if (link.link.to.includes('https://ghost.org/?via=pbg-newsletter')) {
|
|
|
|
poweredByGhostIgnored = false;
|
|
|
|
}
|
2022-10-10 18:15:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(internalRedirectHappened);
|
|
|
|
assert(externalRedirectHappened);
|
2024-03-26 19:51:23 +03:00
|
|
|
assert(poweredByGhostIgnored);
|
2022-10-10 18:15:31 +03:00
|
|
|
|
|
|
|
const {body: {members}} = await agent.get(
|
|
|
|
`/members/`
|
|
|
|
);
|
|
|
|
|
|
|
|
const linkToClick = links[0];
|
|
|
|
const memberToClickLink = members[0];
|
|
|
|
|
|
|
|
const urlOfLinkToClick = new URL(linkToClick.link.from);
|
|
|
|
|
|
|
|
urlOfLinkToClick.searchParams.set('m', memberToClickLink.uuid);
|
|
|
|
|
|
|
|
const previousClickCount = linkToClick.count.clicks;
|
|
|
|
|
|
|
|
await fetchWithoutFollowingRedirect(urlOfLinkToClick.href);
|
|
|
|
|
|
|
|
const {body: {links: [clickedLink]}} = await agent.get(
|
2023-11-21 11:45:36 +03:00
|
|
|
`/links/?filter=${encodeURIComponent(`post_id:'${post.id}'`)}`
|
2022-10-10 18:15:31 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
const clickCount = clickedLink.count.clicks;
|
|
|
|
|
|
|
|
const {body: {events: clickEvents}} = await agent.get(
|
2023-11-21 11:45:36 +03:00
|
|
|
`/members/events/?filter=${encodeURIComponent(`data.member_id:'${memberToClickLink.id}'+type:click_event`)}`
|
2022-10-10 18:15:31 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
const clickEvent = clickEvents.find((/** @type any */ event) => {
|
|
|
|
return event.data.post.id === post.id && event.data.link.from === urlOfLinkToClick.pathname;
|
|
|
|
});
|
|
|
|
|
|
|
|
assert(clickEvent);
|
|
|
|
assert(previousClickCount + 1 === clickCount);
|
|
|
|
});
|
|
|
|
});
|