diff --git a/modules/anonymisedRtdProvider.js b/modules/anonymisedRtdProvider.js index dd8b563d25..da894df014 100644 --- a/modules/anonymisedRtdProvider.js +++ b/modules/anonymisedRtdProvider.js @@ -55,15 +55,20 @@ export function createRtdProvider(moduleName) { return; } logMessage(`${SUBMODULE_NAME}RtdProvider: Loading Marketing Tag`); - // Check if the script is already loaded - if (document.querySelector(`script[src*="${config.params.tagUrl ?? MARKETING_TAG_URL}"]`)) { + let tagBaseUrl = MARKETING_TAG_URL; + if (config.params?.tagUrl) { + logWarn(`${SUBMODULE_NAME}RtdProvider: params.tagUrl is deprecated and will be removed in a future release.`); + tagBaseUrl = config.params.tagUrl; + } + // Check if the script is already loaded (match on host/path only to handle http://, https://, and protocol-relative URLs) + if (document.querySelector(`script[src*="${tagBaseUrl.replace(/^https?:\/\//, '')}"]`)) { logMessage(`${SUBMODULE_NAME}RtdProvider: Marketing Tag already loaded`); return; } const tagConfig = config.params?.tagConfig ? { ...config.params.tagConfig, idw_client_id: config.params.tagConfig.clientId } : {}; delete tagConfig.clientId; - const tagUrl = config.params.tagUrl ? config.params.tagUrl : `${MARKETING_TAG_URL}?ref=prebid&d=${window.location.hostname}`; + const tagUrl = `${tagBaseUrl}?ref=prebid&d=${window.location.hostname}`; loadExternalScript(tagUrl, MODULE_TYPE_RTD, SUBMODULE_NAME, () => { logMessage(`${SUBMODULE_NAME}RtdProvider: Marketing Tag loaded successfully`); diff --git a/modules/anonymisedRtdProvider.md b/modules/anonymisedRtdProvider.md index 0541eeae74..b5830dd864 100644 --- a/modules/anonymisedRtdProvider.md +++ b/modules/anonymisedRtdProvider.md @@ -46,7 +46,7 @@ Anonymised’s Real-time Data Provider automatically obtains segment IDs from th | params.bidders | `Array` | Bidders with which to share segment information | Optional | | params.segtax | `Integer` | The taxonomy for Anonymised | '1000' always | | params.tagConfig | `Object` | Configuration for the Anonymised Marketing Tag | Optional. Defaults to `{}`. | -| params.tagUrl | `String` | The URL of the Anonymised Marketing Tag script | Optional. Defaults to `https://static.anonymised.io/light/loader.js`. | +| params.tagUrl | `String` | The URL of the Anonymised Marketing Tag script | **Deprecated.** Will be removed in a future release. Defaults to `https://static.anonymised.io/light/loader.js`. | The `anonymisedRtdProvider` must be integrated into the publisher's website along with the [Anonymised Marketing Tag](https://support.anonymised.io/integrate/marketing-tag?t=LPukVCXzSIcRoal5jggyeg). One way to install the Marketing Tag is through `anonymisedRtdProvider` by specifying the required [parameters](https://support.anonymised.io/integrate/optional-anonymised-tag-parameters?t=LPukVCXzSIcRoal5jggyeg) in the `tagConfig` object. diff --git a/test/spec/modules/anonymisedRtdProvider_spec.js b/test/spec/modules/anonymisedRtdProvider_spec.js index 6345515931..f975ce2351 100644 --- a/test/spec/modules/anonymisedRtdProvider_spec.js +++ b/test/spec/modules/anonymisedRtdProvider_spec.js @@ -32,6 +32,15 @@ describe('anonymisedRtdProvider', function() { }); describe('anonymisedRtdSubmodule', function() { + let logWarnStub; + beforeEach(function () { + logWarnStub = sinon.stub(require('src/utils.js'), 'logWarn'); + }); + afterEach(function () { + logWarnStub.restore(); + document.querySelectorAll('script[src*="static.anonymised.io"], script[src*="example.io"]').forEach(s => s.parentNode.removeChild(s)); + }); + it('successfully instantiates', function () { expect(anonymisedRtdSubmodule.init()).to.equal(true); }); @@ -115,7 +124,35 @@ describe('anonymisedRtdProvider', function() { anonymisedRtdSubmodule.init(rtdConfig, {}); expect(loadExternalScriptStub.called).to.be.false; }); - it('should load external script from tagUrl when it is set', function () { + it('should not load external script when it is already loaded via http://', function () { + const rtdConfig = { + params: { + tagConfig: { + clientId: 'testId' + } + } + }; + const script = document.createElement('script'); + script.src = 'http://static.anonymised.io/light/loader.js'; + document.body.appendChild(script); + anonymisedRtdSubmodule.init(rtdConfig, {}); + expect(loadExternalScriptStub.called).to.be.false; + }); + it('should not load external script when it is already loaded via protocol-relative URL', function () { + const rtdConfig = { + params: { + tagConfig: { + clientId: 'testId' + } + } + }; + const script = document.createElement('script'); + script.src = '//static.anonymised.io/light/loader.js'; + document.body.appendChild(script); + anonymisedRtdSubmodule.init(rtdConfig, {}); + expect(loadExternalScriptStub.called).to.be.false; + }); + it('should load external script from tagUrl when set and log a deprecation warning', function () { const rtdConfig = { params: { tagUrl: 'https://example.io/loader.js', @@ -125,9 +162,10 @@ describe('anonymisedRtdProvider', function() { } }; anonymisedRtdSubmodule.init(rtdConfig, {}); - const expected = 'https://example.io/loader.js'; + const expected = `https://example.io/loader.js?ref=prebid&d=${window.location.hostname}`; expect(loadExternalScriptStub.args[0][0]).to.deep.equal(expected); + expect(logWarnStub.calledWithMatch('params.tagUrl is deprecated')).to.be.true; }); it('should not load external script from tagUrl when it is already loaded', function () { const rtdConfig = {