Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@telefonica/google-analytics

Package Overview
Dependencies
Maintainers
4
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@telefonica/google-analytics - npm Package Compare versions

Comparing version 1.0.0-beta1 to 1.0.0

dist/api/console.d.ts

71

dist/google-analytics.cjs.development.js

@@ -59,3 +59,3 @@ 'use strict';

var consoleTransport = {
var consoleApi = {
initialize: function initialize() {

@@ -65,11 +65,11 @@ return Promise.resolve();

logEvent: function logEvent(name, params) {
console.log('Analytics event:', name, params);
console.log('[analytics] event:', name, params);
return Promise.resolve();
},
setScreenName: function setScreenName(screenName, params) {
console.log('Analytics screen name:', screenName, params);
console.log('[analytics] screen name:', screenName, params);
return Promise.resolve();
},
setUserProperty: function setUserProperty(name, value) {
console.log('Analytics user property:', name, value);
console.log('[analytics] user property:', name, value);
return Promise.resolve();

@@ -103,9 +103,12 @@ }

};
var gtagTransport = {
var isInitialized = false;
var gtagApi = {
initialize: function initialize(trackingId) {
var scriptId = 'ga';
// make this function idempotent:
if (document.getElementById(scriptId)) return Promise.resolve();
// make this function idempotent. And ignore it in SSR.
if (typeof window === 'undefined' || isInitialized) {
return Promise.resolve();
}
isInitialized = true;
window.dataLayer = window.dataLayer || [];
// WARN: don't refactor this into an arrow funciton with ...args because google analytics won't work!
// WARN: don't refactor this into an arrow function with `...args` because google analytics won't work!
window.gtag = function () {

@@ -123,3 +126,2 @@ window.dataLayer.push(arguments);

var script = document.createElement('script');
script.id = scriptId;
script.type = 'text/javascript';

@@ -152,5 +154,4 @@ script.async = true;

var _excluded = ["name"],
_excluded2 = ["screenName"];
var transport = gtagTransport;
var _excluded = ["name"];
var api = gtagApi;
var handleInitFinished;

@@ -161,18 +162,8 @@ var waitInitialization = /*#__PURE__*/new Promise(function (resolve) {

var initAnalytics = function initAnalytics(trackingId, options) {
if (options != null && options.transport) {
transport = options.transport;
if (options != null && options.api) {
api = options.api;
}
return transport.initialize(trackingId).then(handleInitFinished);
return api.initialize(trackingId).then(handleInitFinished);
};
var currentScreenName;
var logEvent = function logEvent(event) {
return waitInitialization.then(function () {
var _sanitizeAnalyticsPar = sanitizeAnalyticsParams(event),
name = _sanitizeAnalyticsPar.name,
rest = _objectWithoutPropertiesLoose(_sanitizeAnalyticsPar, _excluded);
return transport.logEvent(name, _extends({}, rest, {
screenName: rest.screenName || currentScreenName
}));
});
};
var currentScreenName = '';
var resetScreenName = function resetScreenName() {

@@ -183,22 +174,28 @@ currentScreenName = '';

return waitInitialization.then(function () {
var _sanitizeAnalyticsPar2 = sanitizeAnalyticsParams(_extends({
screenName: screenName
}, params)),
sanitizedScreenName = _sanitizeAnalyticsPar2.screenName,
sanitizedParams = _objectWithoutPropertiesLoose(_sanitizeAnalyticsPar2, _excluded2);
var sanitizedScreenName = sanitizeAnalyticsParam(screenName);
if (sanitizedScreenName === currentScreenName) {
return Promise.resolve();
return;
}
currentScreenName = sanitizedScreenName;
return transport.setScreenName(currentScreenName, sanitizedParams);
return api.setScreenName(sanitizedScreenName, sanitizeAnalyticsParams(params || {}));
});
};
var logEvent = function logEvent(event) {
return waitInitialization.then(function () {
var _sanitizeAnalyticsPar = sanitizeAnalyticsParams(event),
name = _sanitizeAnalyticsPar.name,
rest = _objectWithoutPropertiesLoose(_sanitizeAnalyticsPar, _excluded);
return api.logEvent(name, _extends({}, rest, {
screenName: rest.screenName || currentScreenName
}));
});
};
var setUserProperty = function setUserProperty(name, value) {
return waitInitialization.then(function () {
return transport.setUserProperty(name, sanitizeAnalyticsParam(value));
return api.setUserProperty(name, sanitizeAnalyticsParam(value));
});
};
exports.consoleTransport = consoleTransport;
exports.gtagTransport = gtagTransport;
exports.consoleApi = consoleApi;
exports.gtagApi = gtagApi;
exports.initAnalytics = initAnalytics;

@@ -205,0 +202,0 @@ exports.logEvent = logEvent;

@@ -1,2 +0,2 @@

"use strict";function e(){return(e=Object.assign?Object.assign.bind():function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e}).apply(this,arguments)}function n(e,n){if(null==e)return{};var t,r,o={},i=Object.keys(e);for(r=0;r<i.length;r++)n.indexOf(t=i[r])>=0||(o[t]=e[t]);return o}Object.defineProperty(exports,"__esModule",{value:!0});var t,r,o=function(e){return function(e){return e.normalize("NFD").replace(/[\u0300-\u036f]/g,"")}(e).toLocaleLowerCase().replace(/[^a-z0-9\s\-\_\/\|\:]/g,"").replace(/\s+/g," ").trim().replace(/\s/g,"_").slice(0,100)},i=function(e){var n={};return Object.entries(e).forEach((function(e){var t=e[1],r=e[0].slice(0,40),i="string"==typeof t?o(t):t;n[r]=i})),n},s={initialize:function(){return Promise.resolve()},logEvent:function(e,n){return console.log("Analytics event:",e,n),Promise.resolve()},setScreenName:function(e,n){return console.log("Analytics screen name:",e,n),Promise.resolve()},setUserProperty:function(e,n){return console.log("Analytics user property:",e,n),Promise.resolve()}},a=0,c=function(e){if(a>=3)return e(),function(){};var n=setTimeout((function(){e(),a++}),500);return function(){clearTimeout(n),a=0,e()}},u={initialize:function(e){if(document.getElementById("ga"))return Promise.resolve();window.dataLayer=window.dataLayer||[],window.gtag=function(){window.dataLayer.push(arguments)},window.gtag("js",new Date),window.gtag("config",e,{send_page_view:!1});var n=document.head;if(n){var t=document.createElement("script");t.id="ga",t.type="text/javascript",t.async=!0,t.src="https://www.googletagmanager.com/gtag/js?id="+encodeURIComponent(e),n.insertBefore(t,n.lastChild)}return Promise.resolve()},logEvent:function(n,t){return new Promise((function(r){window.gtag("event",n,e({},t,{event_callback:c(r)}))}))},setScreenName:function(n,t){return this.logEvent("page_view",e({screenName:n,page_title:n},t))},setUserProperty:function(e,n){var t;return window.gtag("set","user_properties",((t={})[e]=n,t)),Promise.resolve()}},l=["name"],f=["screenName"],g=u,p=new Promise((function(e){t=e}));exports.consoleTransport=s,exports.gtagTransport=u,exports.initAnalytics=function(e,n){return null!=n&&n.transport&&(g=n.transport),g.initialize(e).then(t)},exports.logEvent=function(t){return p.then((function(){var o=i(t),s=o.name,a=n(o,l);return g.logEvent(s,e({},a,{screenName:a.screenName||r}))}))},exports.resetScreenName=function(){r=""},exports.sanitizeAnalyticsParam=o,exports.sanitizeAnalyticsParams=i,exports.setScreenName=function(t,o){return p.then((function(){var s=i(e({screenName:t},o)),a=s.screenName,c=n(s,f);return a===r?Promise.resolve():g.setScreenName(r=a,c)}))},exports.setUserProperty=function(e,n){return p.then((function(){return g.setUserProperty(e,o(n))}))};
"use strict";function e(){return(e=Object.assign?Object.assign.bind():function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e}).apply(this,arguments)}Object.defineProperty(exports,"__esModule",{value:!0});var n,t=function(e){return function(e){return e.normalize("NFD").replace(/[\u0300-\u036f]/g,"")}(e).toLocaleLowerCase().replace(/[^a-z0-9\s\-\_\/\|\:]/g,"").replace(/\s+/g," ").trim().replace(/\s/g,"_").slice(0,100)},r=function(e){var n={};return Object.entries(e).forEach((function(e){var r=e[1],o=e[0].slice(0,40),i="string"==typeof r?t(r):r;n[o]=i})),n},o={initialize:function(){return Promise.resolve()},logEvent:function(e,n){return console.log("[analytics] event:",e,n),Promise.resolve()},setScreenName:function(e,n){return console.log("[analytics] screen name:",e,n),Promise.resolve()},setUserProperty:function(e,n){return console.log("[analytics] user property:",e,n),Promise.resolve()}},i=0,a=function(e){if(i>=3)return e(),function(){};var n=setTimeout((function(){e(),i++}),500);return function(){clearTimeout(n),i=0,e()}},s=!1,c={initialize:function(e){if("undefined"==typeof window||s)return Promise.resolve();s=!0,window.dataLayer=window.dataLayer||[],window.gtag=function(){window.dataLayer.push(arguments)},window.gtag("js",new Date),window.gtag("config",e,{send_page_view:!1});var n=document.head;if(n){var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src="https://www.googletagmanager.com/gtag/js?id="+encodeURIComponent(e),n.insertBefore(t,n.lastChild)}return Promise.resolve()},logEvent:function(n,t){return new Promise((function(r){window.gtag("event",n,e({},t,{event_callback:a(r)}))}))},setScreenName:function(n,t){return this.logEvent("page_view",e({screenName:n,page_title:n},t))},setUserProperty:function(e,n){var t;return window.gtag("set","user_properties",((t={})[e]=n,t)),Promise.resolve()}},u=["name"],l=c,f=new Promise((function(e){n=e})),p="";exports.consoleApi=o,exports.gtagApi=c,exports.initAnalytics=function(e,t){return null!=t&&t.api&&(l=t.api),l.initialize(e).then(n)},exports.logEvent=function(n){return f.then((function(){var t=r(n),o=t.name,i=function(e,n){if(null==e)return{};var t,r,o={},i=Object.keys(e);for(r=0;r<i.length;r++)n.indexOf(t=i[r])>=0||(o[t]=e[t]);return o}(t,u);return l.logEvent(o,e({},i,{screenName:i.screenName||p}))}))},exports.resetScreenName=function(){p=""},exports.sanitizeAnalyticsParam=t,exports.sanitizeAnalyticsParams=r,exports.setScreenName=function(e,n){return f.then((function(){var o=t(e);if(o!==p)return p=o,l.setScreenName(o,r(n||{}))}))},exports.setUserProperty=function(e,n){return f.then((function(){return l.setUserProperty(e,t(n))}))};
//# sourceMappingURL=google-analytics.cjs.production.min.js.map

@@ -55,3 +55,3 @@ function _extends() {

var consoleTransport = {
var consoleApi = {
initialize: function initialize() {

@@ -61,11 +61,11 @@ return Promise.resolve();

logEvent: function logEvent(name, params) {
console.log('Analytics event:', name, params);
console.log('[analytics] event:', name, params);
return Promise.resolve();
},
setScreenName: function setScreenName(screenName, params) {
console.log('Analytics screen name:', screenName, params);
console.log('[analytics] screen name:', screenName, params);
return Promise.resolve();
},
setUserProperty: function setUserProperty(name, value) {
console.log('Analytics user property:', name, value);
console.log('[analytics] user property:', name, value);
return Promise.resolve();

@@ -99,9 +99,12 @@ }

};
var gtagTransport = {
var isInitialized = false;
var gtagApi = {
initialize: function initialize(trackingId) {
var scriptId = 'ga';
// make this function idempotent:
if (document.getElementById(scriptId)) return Promise.resolve();
// make this function idempotent. And ignore it in SSR.
if (typeof window === 'undefined' || isInitialized) {
return Promise.resolve();
}
isInitialized = true;
window.dataLayer = window.dataLayer || [];
// WARN: don't refactor this into an arrow funciton with ...args because google analytics won't work!
// WARN: don't refactor this into an arrow function with `...args` because google analytics won't work!
window.gtag = function () {

@@ -119,3 +122,2 @@ window.dataLayer.push(arguments);

var script = document.createElement('script');
script.id = scriptId;
script.type = 'text/javascript';

@@ -148,5 +150,4 @@ script.async = true;

var _excluded = ["name"],
_excluded2 = ["screenName"];
var transport = gtagTransport;
var _excluded = ["name"];
var api = gtagApi;
var handleInitFinished;

@@ -157,18 +158,8 @@ var waitInitialization = /*#__PURE__*/new Promise(function (resolve) {

var initAnalytics = function initAnalytics(trackingId, options) {
if (options != null && options.transport) {
transport = options.transport;
if (options != null && options.api) {
api = options.api;
}
return transport.initialize(trackingId).then(handleInitFinished);
return api.initialize(trackingId).then(handleInitFinished);
};
var currentScreenName;
var logEvent = function logEvent(event) {
return waitInitialization.then(function () {
var _sanitizeAnalyticsPar = sanitizeAnalyticsParams(event),
name = _sanitizeAnalyticsPar.name,
rest = _objectWithoutPropertiesLoose(_sanitizeAnalyticsPar, _excluded);
return transport.logEvent(name, _extends({}, rest, {
screenName: rest.screenName || currentScreenName
}));
});
};
var currentScreenName = '';
var resetScreenName = function resetScreenName() {

@@ -179,21 +170,27 @@ currentScreenName = '';

return waitInitialization.then(function () {
var _sanitizeAnalyticsPar2 = sanitizeAnalyticsParams(_extends({
screenName: screenName
}, params)),
sanitizedScreenName = _sanitizeAnalyticsPar2.screenName,
sanitizedParams = _objectWithoutPropertiesLoose(_sanitizeAnalyticsPar2, _excluded2);
var sanitizedScreenName = sanitizeAnalyticsParam(screenName);
if (sanitizedScreenName === currentScreenName) {
return Promise.resolve();
return;
}
currentScreenName = sanitizedScreenName;
return transport.setScreenName(currentScreenName, sanitizedParams);
return api.setScreenName(sanitizedScreenName, sanitizeAnalyticsParams(params || {}));
});
};
var logEvent = function logEvent(event) {
return waitInitialization.then(function () {
var _sanitizeAnalyticsPar = sanitizeAnalyticsParams(event),
name = _sanitizeAnalyticsPar.name,
rest = _objectWithoutPropertiesLoose(_sanitizeAnalyticsPar, _excluded);
return api.logEvent(name, _extends({}, rest, {
screenName: rest.screenName || currentScreenName
}));
});
};
var setUserProperty = function setUserProperty(name, value) {
return waitInitialization.then(function () {
return transport.setUserProperty(name, sanitizeAnalyticsParam(value));
return api.setUserProperty(name, sanitizeAnalyticsParam(value));
});
};
export { consoleTransport, gtagTransport, initAnalytics, logEvent, resetScreenName, sanitizeAnalyticsParam, sanitizeAnalyticsParams, setScreenName, setUserProperty };
export { consoleApi, gtagApi, initAnalytics, logEvent, resetScreenName, sanitizeAnalyticsParam, sanitizeAnalyticsParams, setScreenName, setUserProperty };
//# sourceMappingURL=google-analytics.esm.js.map
import { sanitizeAnalyticsParam, sanitizeAnalyticsParams } from './sanitize';
import { consoleTransport } from './transport/console';
import { gtagTransport } from './transport/gtag';
import type { AnalyticsTransport } from './transport/type';
export type { AnalyticsTransport };
export { gtagTransport, consoleTransport, sanitizeAnalyticsParam, sanitizeAnalyticsParams };
import { consoleApi } from './api/console';
import { gtagApi } from './api/gtag';
import type { AnalyticsApi } from './api/type';
export type { AnalyticsApi };
export { gtagApi, consoleApi, sanitizeAnalyticsParam, sanitizeAnalyticsParams };
export declare type TrackingEvent = {

@@ -17,5 +17,4 @@ name: string;

export declare const initAnalytics: (trackingId: string, options?: {
transport?: AnalyticsTransport | undefined;
api?: AnalyticsApi | undefined;
} | undefined) => Promise<void>;
export declare const logEvent: (event: TrackingEvent) => Promise<void>;
export declare const resetScreenName: () => void;

@@ -25,2 +24,3 @@ export declare const setScreenName: (screenName: string, params?: {

} | undefined) => Promise<void>;
export declare const logEvent: (event: TrackingEvent) => Promise<void>;
export declare const setUserProperty: (name: string, value: string) => Promise<void>;
{
"name": "@telefonica/google-analytics",
"type": "module",
"version": "1.0.0-beta1",
"version": "1.0.0",
"description": "",

@@ -6,0 +6,0 @@ "exports": {

@@ -41,16 +41,27 @@ # `@telefonica/google-analytics`

## Use custom transport
## Use custom Api
Events are sent to Google servers by default (using `gtagTransport`), but you can use a custom transport to
change this behavior. For example, Novum app uses the webview bridge to send the events to the native app, and
then the native app sends the events to Firebase.
Events are sent to Google servers by default (using `gtagApi`), but you can use a custom api to change this
behavior. For example, Novum app uses the webview bridge to send the events to the native app, and then the
native app sends the events to Firebase.
Another use case of custom transport is to log events in the browser console in development mode:
Another use case of custom api is to log events in the browser console in development mode:
```ts
import {initAnalytics, consoleTransport, gtagTransport} from '@telefonica/google-analytics';
import {initAnalytics, consoleApi, gtagApi} from '@telefonica/google-analytics';
initAnalytics('G-XXXXXXXXXX', {
transport: process.env.NODE_ENV === 'production' ? gtagTransport : consoleTransport,
api: process.env.NODE_ENV === 'production' ? gtagApi : consoleApi,
});
```
## CSP
If you are using a CSP, you must add the following directives for Google Analytics to work:
```
script-src https://*.googletagmanager.com; connect-src https://*.google-analytics.com https://*.analytics.google.com https://*.googletagmanager.com; img-src https://*.google-analytics.com https://*.googletagmanager.com;
```
Check Google docs for
[CSP in Google Analytics 4](https://developers.google.com/tag-platform/tag-manager/csp#google_analytics_4_google_analytics)

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc