Skip to content

Commit

Permalink
6.14.0
Browse files Browse the repository at this point in the history
  • Loading branch information
piano-analytics committed Jan 31, 2024
1 parent 5c200ee commit bdd24e4
Show file tree
Hide file tree
Showing 13 changed files with 424 additions and 357 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
# Changelog
## 6.14.0
### New
- [browser] New consent methods using `purposes`

### Changes
- All events properties can now be overridden (using setProperty(ies) or specifying the property directly in events sent)

### Fixes
- Fixed a conflict with global variables used in multiple taggings

## 6.13.1
### Fixes
- [browser] Cookie testing is now secure on suitable URLs
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "piano-analytics-js",
"description": "JavaScript library for Piano Analytics",
"version": "6.13.1",
"version": "6.14.0",
"main": "dist/browserless/piano-analytics.cjs.js",
"module": "dist/browserless/piano-analytics.esm.js",
"browser": "dist/browser/piano-analytics.umd.js",
Expand Down Expand Up @@ -37,13 +37,13 @@
"test:node": "npm run rollup:node && node test/node.run.js"
},
"devDependencies": {
"@babel/core": "7.22.15",
"@babel/preset-env": "7.22.15",
"@rollup/plugin-babel": "6.0.3",
"@babel/core": "7.23.7",
"@babel/preset-env": "7.23.8",
"@rollup/plugin-babel": "6.0.4",
"chai": "4.3.8",
"eslint": "8.48.0",
"eslint": "8.56.0",
"eslint-config-standard": "17.1.0",
"eslint-plugin-import": "2.28.1",
"eslint-plugin-import": "2.29.1",
"eslint-plugin-node": "11.1.0",
"eslint-plugin-promise": "6.1.1",
"grunt": "1.6.1",
Expand All @@ -55,7 +55,7 @@
"karma-mocha": "2.0.1",
"load-grunt-tasks": "5.1.0",
"mocha": "10.2.0",
"puppeteer": "19.6.2",
"puppeteer": "21.9.0",
"rollup": "2.79.1",
"rollup-plugin-eslint": "7.0.0",
"rollup-plugin-replace": "2.2.0",
Expand Down
19 changes: 17 additions & 2 deletions src/business/privacy/dl-privacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ function DlPrivacy(pa) {
'visitor_privacy_mode': 'custom'
}
};
const isConsentv2 = function () {
return window.pdl.requireConsent === 'v2';
};

this.init = function () {
this.consentItems = getConsentItems();
Expand All @@ -54,7 +57,7 @@ function DlPrivacy(pa) {
productName: 'PA',
items: this.consentItems.cookieItems
});
if(!pa.getConfiguration('isLegacyPrivacy')){
if (!pa.getConfiguration('isLegacyPrivacy')) {
this.initMode();
this.filterKeys();
}
Expand Down Expand Up @@ -91,6 +94,19 @@ function DlPrivacy(pa) {
this.modeMetadata['custom'].visitor_privacy_mode = modeName || 'custom';
this.modeMetadata['custom'].visitor_privacy_consent = consentValue;
};
this.setAllPurposes = function (mode) {
if(isConsentv2()){
return dataLayer.utils.setConsent(mode);
}
};
this.setByPurpose = function (purpose, mode, products) {
if(isConsentv2()){
dataLayer.utils.setConsent(purpose, mode, products);
}
};
this.getByPurpose = function () {
return dataLayer.utils.getConsent();
};

/* internal use */
this.getModeMetadata = function () {
Expand Down Expand Up @@ -136,7 +152,6 @@ function DlPrivacy(pa) {
}
}
};

this.setItem = function (key, value, expiration, callback) {
if (this.isKeyAllowed(key)) {
pa._storage.setItem(key, value, expiration, callback);
Expand Down
2 changes: 1 addition & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default {
],
'storageVisitor': 'pa_vid',
'storageUser': 'pa_user',
'version': '6.13.1',
'version': '6.14.0',
'minHeartbeat': 5,
'minBufferingHeartbeat': 1,
'queueVarName': '_paq',
Expand Down
2 changes: 1 addition & 1 deletion src/core/PianoAnalytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function _sendEvent(events, options) {
}
events[i] = eventFormatted;
}
const data = {events: events, options: options};
const data = {events: cloneObject(events), options: cloneObject(options)};
if (steps.length > 0 && typeof steps[0] === 'function') {
const clonedConfig = new Configuration(this.cfg.cloneData());
steps[0](this, new Model(this, data, clonedConfig), steps.slice(1));
Expand Down
32 changes: 27 additions & 5 deletions src/core/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import {cloneObject} from '../utils/index';

function Model(pa, data, config) {
this.properties = cloneObject(pa._properties);
this.setProperty = function (name, value, options) {
this.addEventsProperty = function (name, value) {
if (pa._privacy.call('isPropAllowed', name)) {
this.properties[name] = {
value: value,
options: options || {}
};
for (const event of this.events) {
if (this.isPropertyAbsentForEvent(name, event)) {
event.data[name] = value;
}
}
}
};

this.hasProperty = function (name) {
return Object.prototype.hasOwnProperty.call(this.properties, name);
};
Expand All @@ -22,6 +24,26 @@ function Model(pa, data, config) {
data: {}
};
this.events = data.events || [];
this.isPropertyAbsentForEvent = function (name, event) {
if (typeof event.data[name] !== 'undefined') {
return false;
} else if (this.hasProperty(name)) {
if (typeof this.properties[name].options.events !== 'undefined') {
const propertyEventsOption = this.properties[name].options.events;
for (const eventAllowed of propertyEventsOption) {
if (
event.name === eventAllowed ||
(eventAllowed.charAt(eventAllowed.length - 1) === '*' && event.name.indexOf(eventAllowed.substring(0, eventAllowed.length - 1)) === 0)
) {
return false;
}
}
} else {
return false;
}
}
return true;
};
}

export default Model;
2 changes: 1 addition & 1 deletion src/core/steps/campaigns.step.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function _addCampaignParams(pa, model, href, prefix, destPrefix) {
let found = false;
for (const param in campaignParams) {
if (Object.prototype.hasOwnProperty.call(campaignParams, param) && !model.properties[param]) {
model.setProperty(param, campaignParams[param], {persistent: true});
model.addEventsProperty(param, campaignParams[param], {persistent: true});
}
found = true;
}
Expand Down
67 changes: 20 additions & 47 deletions src/core/steps/metadata.step.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ function _camelToSnake(string) {
}

function metadataStep(pa, model, nextSteps) {
model.setProperty('event_collection_platform', BUILD_BROWSER ? 'js' : 'js-browserless');
model.setProperty('event_collection_version', model.getConfiguration('version'));
model.addEventsProperty('event_collection_platform', BUILD_BROWSER ? 'js' : 'js-browserless');
model.addEventsProperty('event_collection_version', model.getConfiguration('version'));
const date = new Date();
model.setProperty('device_timestamp_utc', date.getTime());
model.setProperty('device_local_hour', date.getTime());
model.setProperty('device_hour', date.getHours());
model.addEventsProperty('device_timestamp_utc', date.getTime());
model.addEventsProperty('device_local_hour', date.getTime());
model.addEventsProperty('device_hour', date.getHours());

if (BUILD_BROWSER) {
const manualConfig = 'isManualPageRefresh',
Expand All @@ -43,20 +43,14 @@ function metadataStep(pa, model, nextSteps) {
}

}
if (_isPropertiesAbsentForEvent(pageviewidProp, model, event)) {
if (pa._privacy.call('isPropAllowed', pageviewidProp)) {
event.data[pageviewidProp] = dataLayer.get('pageViewId');
}
if (pa._privacy.call('isPropAllowed', pageviewidProp) && model.isPropertyAbsentForEvent(pageviewidProp, event)) {
event.data[pageviewidProp] = dataLayer.get('pageViewId');
}
}

try {
const cookieCreationDate = new Date((new Date(dataLayer.cookies._pcid.fixedAt[0])).setUTCHours(0, 0, 0, 0)).toISOString();
for (const event of model.events) {
if (_isPropertiesAbsentForEvent('cookie_creation_date', model, event)) {
model.setProperty('cookie_creation_date', cookieCreationDate);
}
}
model.addEventsProperty('cookie_creation_date', cookieCreationDate);
} catch (e) { /* empty */
}
const content = dataLayer.get('content');
Expand All @@ -67,40 +61,34 @@ function metadataStep(pa, model, nextSteps) {
'tags': 'tags_array'
};
const propFinalName = (propContent === 'createdAt' || propContent === 'tags') ? MAP_PA_DL[propContent] : _camelToSnake(`content_${propContent}`);
for (const event of model.events) {
if (_isPropertiesAbsentForEvent(propFinalName, model, event)) {
if (pa._privacy.call('isPropAllowed', propFinalName)) {
event.data[propFinalName] = content[propContent];
}
}
}
model.addEventsProperty(propFinalName, content[propContent]);
}
}

model.setProperty('has_access', dataLayer.get('userStatus'));
model.setProperty('device_screen_width', window.screen.width);
model.setProperty('device_screen_height', window.screen.height);
model.setProperty('device_display_width',
model.addEventsProperty('has_access', dataLayer.get('userStatus'));
model.addEventsProperty('device_screen_width', window.screen.width);
model.addEventsProperty('device_screen_height', window.screen.height);
model.addEventsProperty('device_display_width',
window.innerWidth ||
document.documentElement && document.documentElement.clientWidth ?
document.documentElement.clientWidth : ''
);
model.setProperty('device_display_height',
model.addEventsProperty('device_display_height',
window.innerHeight ||
document.documentElement && document.documentElement.clientHeight ?
document.documentElement.clientHeight : ''
);
const language = window.navigator ? (window.navigator.language || window.navigator.userLanguage) : '';
const languageSplitted = _parseLanguage(language, ['-', '_']);
model.setProperty('browser_language', languageSplitted[0]);
model.setProperty('browser_language_local', languageSplitted[1]);
model.setProperty('previous_url', document.referrer || '');
model.addEventsProperty('browser_language', languageSplitted[0]);
model.addEventsProperty('browser_language_local', languageSplitted[1]);
model.addEventsProperty('previous_url', document.referrer || '');
if (document.title) {
model.setProperty('page_title_html', document.title);
model.addEventsProperty('page_title_html', document.title);
}
const eventUrlWithQueryString = model.getConfiguration('addEventURL').toString() === 'true';
if (eventUrlWithQueryString || (model.getConfiguration('addEventURL') === 'withoutQS')) {
model.setProperty('event_url_full', eventUrlWithQueryString ? window.location.href.split('#')[0] : `${window.location.protocol}//${window.location.host}${window.location.pathname}`);
model.addEventsProperty('event_url_full', eventUrlWithQueryString ? window.location.href.split('#')[0] : `${window.location.protocol}//${window.location.host}${window.location.pathname}`);
}

try {
Expand Down Expand Up @@ -139,21 +127,6 @@ function metadataStep(pa, model, nextSteps) {
}
}

function _isPropertiesAbsentForEvent(name, model, event) {
if (model.hasProperty(name) && model.properties[name].options.events) {
const propertyEventsOption = model.properties[name].options.events;
if (propertyEventsOption.indexOf(event.name) > -1) {
return false;
}
for (const eventAllowed of propertyEventsOption) {
if (eventAllowed.charAt(eventAllowed.length - 1) === '*' && event.name.indexOf(eventAllowed.substring(0, eventAllowed.length - 1)) === 0) {
return false;
}
}
}
return typeof event.data[name] === 'undefined';
}

function _isDefined(variable) {
return typeof variable !== 'undefined';
}
Expand Down Expand Up @@ -200,7 +173,7 @@ function _addUserAgentMetadata(model, ua) {
if (_isDefined(ua)) {
for (let i = 0; i < properties.length; i++) {
if (_isDefined(ua[properties[i].metric])) {
model.setProperty(properties[i].property, ua[properties[i].metric]);
model.addEventsProperty(properties[i].property, ua[properties[i].metric]);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/steps/privacy.step.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function privacyStep(pa, model, nextSteps) {
let metadata = pa._privacy.call('getModeMetadata') || {};
for (const property in metadata) {
if (Object.prototype.hasOwnProperty.call(metadata, property)) {
model.setProperty(property, metadata[property]);
model.addEventsProperty(property, metadata[property]);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/steps/properties.step.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function propertiesStep(pa, model, nextSteps) {
} else if (!propertyEventsOption) {
isEventOptionOk = true;
}
if (isEventOptionOk) {
if (isEventOptionOk && typeof event.data[property] === 'undefined') {
event.data[property] = model.properties[property].value;
isAdded = true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/steps/user.step.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ function userStep(pa, model, nextSteps) {
const opts = {
persistent: true
};
model.setProperty('user_id', userStored.id, opts);
model.setProperty('user_category', userStored.category, opts);
model.setProperty('user_recognition', true, opts);
model.addEventsProperty('user_id', userStored.id, opts);
model.addEventsProperty('user_category', userStored.category, opts);
model.addEventsProperty('user_recognition', true, opts);
}
nextStep(pa, model, nextSteps);
});
Expand Down
Loading

0 comments on commit bdd24e4

Please sign in to comment.