• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

prebid / Prebid.js / #304

24 Jun 2025 05:14PM UTC coverage: 90.422% (+0.02%) from 90.403%
#304

push

travis-ci

prebidjs-release
Prebid 9.51.0 release

43346 of 54399 branches covered (79.68%)

64092 of 70881 relevant lines covered (90.42%)

174.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.01
/modules/pubmaticIdSystem.js
1
import { logInfo, logError, isStr, isEmptyStr } from '../src/utils.js';
1✔
2
import { ajax } from '../src/ajax.js';
3
import { submodule } from '../src/hook.js';
4
import { getStorageManager } from '../src/storageManager.js';
5
import { MODULE_TYPE_UID } from '../src/activities/modules.js';
6
import { uspDataHandler, coppaDataHandler, gppDataHandler, gdprDataHandler } from '../src/adapterManager.js';
7

8
const MODULE_NAME = 'pubmaticId';
1✔
9
const GVLID = 76;
1✔
10
export const STORAGE_NAME = 'pubmaticId';
1✔
11
const STORAGE_EXPIRES = 30; // days
1✔
12
const STORAGE_REFRESH_IN_SECONDS = 24 * 3600; // 24 Hours
1✔
13
const LOG_PREFIX = 'PubMatic User ID: ';
1✔
14
const VERSION = '1';
1✔
15
const API_URL = 'https://image6.pubmatic.com/AdServer/UCookieSetPug?oid=5&p=';
1✔
16

17
export const storage = getStorageManager({ moduleType: MODULE_TYPE_UID, moduleName: MODULE_NAME });
1✔
18

19
function generateQueryStringParams(config, consentData) {
20
  const uspString = uspDataHandler.getConsentData();
7✔
21
  const coppaValue = coppaDataHandler.getCoppa();
7✔
22
  const gppConsent = gppDataHandler.getConsentData();
7✔
23
  const gdprConsent = gdprDataHandler.getConsentData();
7✔
24

25
  const params = {
7✔
26
    publisherId: Number(config.params.publisherId),
27
    gdpr: (gdprConsent && gdprConsent?.gdprApplies) ? 1 : 0,
16✔
28
    gdpr_consent: gdprConsent && gdprConsent?.consentString ? encodeURIComponent(gdprConsent.consentString) : '',
16✔
29
    src: 'pbjs_uid',
30
    ver: VERSION,
31
    coppa: Number(coppaValue),
32
    us_privacy: uspString ? encodeURIComponent(uspString) : '',
7✔
33
    gpp: gppConsent?.gppString ? encodeURIComponent(gppConsent.gppString) : '',
7✔
34
    gpp_sid: gppConsent?.applicableSections?.length ? encodeURIComponent(gppConsent.applicableSections.join(',')) : ''
7✔
35
  };
36

37
  return params;
7✔
38
}
39

40
function buildUrl(config, consentData) {
41
  let baseUrl = `${API_URL}${config.params.publisherId}`;
7✔
42
  const params = generateQueryStringParams(config, consentData);
7✔
43

44
  Object.keys(params).forEach((key) => {
7✔
45
    baseUrl += `&${key}=${params[key]}`;
63✔
46
  });
47

48
  return baseUrl;
7✔
49
}
50

51
function deleteFromAllStorages(key) {
52
  const cKeys = [key, `${key}_cst`, `${key}_last`, `${key}_exp`];
×
53
  cKeys.forEach((cKey) => {
×
54
    if (storage.getCookie(cKey)) {
×
55
      storage.setCookie(cKey, '', new Date(0).toUTCString());
×
56
    }
57
  });
58

59
  const lsKeys = [key, `${key}_cst`, `${key}_last`, `${key}_exp`];
×
60
  lsKeys.forEach((lsKey) => {
×
61
    if (storage.getDataFromLocalStorage(lsKey)) {
×
62
      storage.removeDataFromLocalStorage(lsKey);
×
63
    }
64
  });
65
}
66

67
function getSuccessAndErrorHandler(callback) {
68
  return {
7✔
69
    success: (response) => {
70
      let responseObj;
71

72
      try {
1✔
73
        responseObj = JSON.parse(response);
1✔
74
        logInfo(LOG_PREFIX + 'response received from the server', responseObj);
1✔
75
      } catch (error) {}
76

77
      if (responseObj && isStr(responseObj.id) && !isEmptyStr(responseObj.id)) {
1!
78
        callback(responseObj);
1✔
79
      } else {
80
        deleteFromAllStorages(STORAGE_NAME);
×
81
        callback();
×
82
      }
83
    },
84
    error: (error) => {
85
      deleteFromAllStorages(STORAGE_NAME);
×
86
      logError(LOG_PREFIX + 'getId fetch encountered an error', error);
×
87
      callback();
×
88
    }
89
  };
90
}
91

92
function hasRequiredConfig(config) {
93
  if (!config || !config.storage || !config.params) {
8✔
94
    logError(LOG_PREFIX + 'config.storage and config.params should be passed.');
1✔
95
    return false;
1✔
96
  }
97

98
  // convert publisherId to number
99
  if (config.params.publisherId) {
7!
100
    config.params.publisherId = Number(config.params.publisherId);
7✔
101
  }
102

103
  if (!config.params.publisherId) {
7!
104
    logError(LOG_PREFIX + 'config.params.publisherId (Number) should be provided.');
×
105
    return false;
×
106
  }
107

108
  if (config.storage.name !== STORAGE_NAME) {
7!
109
    logError(LOG_PREFIX + `config.storage.name should be '${STORAGE_NAME}'.`);
×
110
    return false;
×
111
  }
112

113
  if (config.storage.expires !== STORAGE_EXPIRES) {
7!
114
    logError(LOG_PREFIX + `config.storage.expires should be ${STORAGE_EXPIRES}.`);
×
115
    return false;
×
116
  }
117

118
  if (config.storage.refreshInSeconds !== STORAGE_REFRESH_IN_SECONDS) {
7!
119
    logError(LOG_PREFIX + `config.storage.refreshInSeconds should be ${STORAGE_REFRESH_IN_SECONDS}.`);
×
120
    return false;
×
121
  }
122

123
  return true;
7✔
124
}
125

126
export const pubmaticIdSubmodule = {
1✔
127
  name: MODULE_NAME,
128
  gvlid: GVLID,
129
  decode(value) {
130
    if (isStr(value.id) && !isEmptyStr(value.id)) {
1!
131
      return { pubmaticId: value.id };
1✔
132
    }
133
    return undefined;
×
134
  },
135
  getId(config, consentData) {
136
    if (!hasRequiredConfig(config)) {
8✔
137
      return undefined;
1✔
138
    }
139

140
    const resp = (callback) => {
7✔
141
      logInfo(LOG_PREFIX + 'requesting an ID from the server');
7✔
142
      const url = buildUrl(config, consentData);
7✔
143
      ajax(url, getSuccessAndErrorHandler(callback), null, {
7✔
144
        method: 'GET',
145
        withCredentials: true,
146
      });
147
    };
148

149
    return { callback: resp };
7✔
150
  },
151
  eids: {
152
    'pubmaticId': {
153
      source: 'esp.pubmatic.com',
154
      atype: 1,
155
      getValue: (data) => {
156
        return data;
1✔
157
      }
158
    },
159
  }
160
};
161

162
submodule('userId', pubmaticIdSubmodule);
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc