Skip to content

Commit

Permalink
Not saving click data for internal links when sub-domain changes
Browse files Browse the repository at this point in the history
  • Loading branch information
liljenback committed May 15, 2024
1 parent a534bde commit e8aeaa4
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ governing permissions and limitations under the License.
*/

import activityMapExtensionEnabled from "./utils/activityMapExtensionEnabled";
import isDifferentDomains from "./utils/isDifferentDomains";

const isDissallowedLinkType = (clickCollection, linkType) => {
return (
Expand Down Expand Up @@ -60,7 +61,8 @@ export default ({
// priority over onBeforeLinkClickSend and only supports processing click properties.
elementProperties.isInternalLink() &&
clickCollection.eventGroupingEnabled &&
(!config.onBeforeLinkClickSend || clickCollection.filterClickDetails)
(!config.onBeforeLinkClickSend || clickCollection.filterClickDetails) &&
!isDifferentDomains(window.location.hostname, elementProperties.linkUrl)
) {
clickActivityStorage.save(elementProperties.properties);
} else if (elementProperties.isValidLink()) {
Expand Down
20 changes: 20 additions & 0 deletions src/components/ActivityCollector/utils/dom/extractDomain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Copyright 2024 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

export default uri => {
let fullUrl = uri;
if (!/^https?:\/\//i.test(fullUrl)) {
fullUrl = `${window.location.protocol}//${uri}`;
}
const url = new URL(fullUrl);
return url.hostname;
};
19 changes: 19 additions & 0 deletions src/components/ActivityCollector/utils/isDifferentDomains.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Copyright 2024 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

import extractDomain from "./dom/extractDomain";

export default (uri1, uri2) => {
const domain1 = extractDomain(uri1);
const domain2 = extractDomain(uri2);
return domain1 !== domain2;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright 2024 example. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

import extractDomain from "../../../../../../../src/components/ActivityCollector/utils/dom/extractDomain";

describe("ActivityCollector::extractDomain", () => {
it("should extract the domain from a URL", () => {
expect(extractDomain("www.example.com")).toBe("www.example.com");
expect(extractDomain("http://www.example.com")).toBe("www.example.com");
expect(extractDomain("https://www.example.com")).toBe("www.example.com");
expect(extractDomain("https://www.example.com/")).toBe("www.example.com");
expect(extractDomain("https://www.example.com/cool/page")).toBe(
"www.example.com"
);
});

it("should handle URLs without a protocol", () => {
expect(extractDomain("example.com")).toBe("example.com");
expect(extractDomain("www.example.com")).toBe("www.example.com");
expect(extractDomain("www.example.com/")).toBe("www.example.com");
expect(extractDomain("www.example.com/cool/page")).toBe("www.example.com");
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2024 example. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

import isDifferentDomains from "../../../../../../src/components/ActivityCollector/utils/isDifferentDomains";

describe("ActivityCollector::isDifferentDomains", () => {
it("should return true if the domains are different", () => {
expect(isDifferentDomains("www.example.com", "www.example.org")).toBe(true);
});

it("should return false if the domains are the same", () => {
expect(
isDifferentDomains("https://www.example.com", "www.example.com")
).toBe(false);
expect(isDifferentDomains("www.example.com", "www.example.com")).toBe(
false
);
});
});

0 comments on commit e8aeaa4

Please sign in to comment.