Skip to content

Commit

Permalink
fix: enable eslint and follow it's errors
Browse files Browse the repository at this point in the history
  • Loading branch information
auto200 committed Jan 31, 2024
1 parent c8b47cc commit ab25dd2
Show file tree
Hide file tree
Showing 23 changed files with 113 additions and 110 deletions.
34 changes: 0 additions & 34 deletions .eslintrc

This file was deleted.

16 changes: 16 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/** @type {import('eslint').Linter.Config} */
const config = {
parser: '@typescript-eslint/parser',
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended-type-checked',
],
plugins: ['@typescript-eslint'],
ignorePatterns:[".eslintrc.cjs", "example"],
parserOptions: {
project:true,
tsconfigRootDir: __dirname,
},
}

module.exports = config
2 changes: 1 addition & 1 deletion example/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { SnackbarProvider } from 'notistack'
import PiwikPro from '@piwikpro/react-piwik-pro'

PiwikPro.initialize(
'0c0a8661-8c10-4f59-b8fc-1c926cbac184',
'957285ba-7867-4c6b-a8be-5e3e6c069b71',
'https://astralprojection.promilci.com'
)

Expand Down
13 changes: 3 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"test:unit": "cross-env CI=1 react-scripts test --env=jsdom",
"test:watch": "react-scripts test --env=jsdom",
"predeploy": "cd example && yarn install && yarn run build",
"deploy": "gh-pages -d example/build"
"deploy": "gh-pages -d example/build",
"format":"prettier src/**/*.ts --list-different",
"format:fix":"npm run format --write"
},
"peerDependencies": {
"react": "^16.8.0 || ^17.0.0 || ^18.0.0",
Expand All @@ -41,15 +43,6 @@
"babel-eslint": "^10.1.0",
"cross-env": "^7.0.3",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-standard": "^17.1.0",
"eslint-config-standard-react": "^13.0.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^5.1.2",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-standard": "^5.0.0",
"gh-pages": "^6.1.1",
"microbundle-crl": "^0.13.11",
"npm-run-all": "^4.1.5",
Expand Down
4 changes: 1 addition & 3 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { PiwikProWindow } from '../interfaces/piwikpro.window'

function init(containerId: string, containerUrl: string, nonce?: string) {
if (!containerId) {
console.error('Empty tracking code for Piwik Pro.')
Expand Down Expand Up @@ -37,7 +35,7 @@ tags.async=!0,tags.src="${containerUrl}/"+id+".js"+qPString,scripts.parentNode.i

export const IS_DEBUG =
(typeof process !== 'undefined' && process.env.NODE_ENV === 'development') ||
(typeof window !== 'undefined' && (window as PiwikProWindow).IS_DEBUG) ||
(typeof window !== 'undefined' && window.IS_DEBUG) ||
false

export default {
Expand Down
File renamed without changes.
8 changes: 5 additions & 3 deletions src/interfaces/piwikpro.window.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export interface PiwikProWindow extends Window {
_paq?: any;
dataLayer?: any;
import { AnyData, QueueItem } from './utils';

export interface PiwikProWindow {
_paq?: QueueItem[];
dataLayer?: AnyData[];
IS_DEBUG?: boolean;
}
11 changes: 11 additions & 0 deletions src/interfaces/tracker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { VisitorInfo } from "./visitorInfo";

export type Tracker = {
hasCookies: () => boolean;
getCustomDimensionValue: (id: string | number) => string | undefined;
getLinkTrackingTimer: () => number;
getEcommerceItems: () => object;
getUserId: () => string;
getVisitorId: () => string;
getVisitorInfo:()=> VisitorInfo
};
10 changes: 10 additions & 0 deletions src/interfaces/utils.ts
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
import { TRACK_EVENT } from "../constants/track-event.constant";
import { Tracker } from "./tracker";

export type LimitedArrayFiveStrings<T extends string[] = []> = [string, ...T] | [string, string, string, string, string];

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type AnyData = any

export type Dimensions = Record<`dimension${number}`,string>

export type QueueItem =[TRACK_EVENT, ...unknown[]] | [(this: Tracker) => void]
9 changes: 9 additions & 0 deletions src/interfaces/visitorInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type VisitorInfo = [
isNew: '0' | '1',
visitorId: string,
firstVisitTS: number,
previousVisitCount: string | number,
currentVisitTS: number,
lastVisitTS: number | '',
lastEcommerceOrderTS: number | ''
]
8 changes: 4 additions & 4 deletions src/services/content-tracking/contentTracking.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ export function trackAllContentImpressions() {
]);
}

export function trackVisibleContentImpressions(checkOnScroll?: boolean, watchIterval?: number) {
export function trackVisibleContentImpressions(checkOnScroll?: boolean, watchInterval?: number) {
PaqService.push([
TRACK_EVENT.VISIBLE_CONTENT_IMPRESSIONS,
checkOnScroll,
watchIterval,
watchInterval,
]);
}

export function trackContentImpressionsWithinNode(domNode: any) {
export function trackContentImpressionsWithinNode(domNode: Node) {
PaqService.push([
TRACK_EVENT.CONTENT_IMPRESSIONS_WITH_NODE,
domNode
Expand All @@ -35,7 +35,7 @@ export function logAllContentBlocksOnPage(): void {
PaqService.push([ TRACK_EVENT.LOG_ALL_CONTENT_BLOCKS_ON_PAGE ]);
}

export function trackContentInteractionNode(domNode: any, contentInteraction: string) {
export function trackContentInteractionNode(domNode: Node, contentInteraction: string) {
PaqService.push([
TRACK_EVENT.CONTENT_INTERACTION_NODE,
domNode,
Expand Down
3 changes: 2 additions & 1 deletion src/services/cookie-management/cookieManagement.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TRACK_EVENT } from '../../constants/track-event.constant';
import { Tracker } from '../../interfaces/tracker';
import { PaqService } from '../paqService/paq.service';

export function disableCookies() {
Expand All @@ -20,7 +21,7 @@ export function hasCookies(): Promise<boolean> {
return new Promise((resolve, reject) => {
try {
PaqService.push([
function (this: any) {
function (this: Tracker) {
resolve(this.hasCookies());
},
]);
Expand Down
5 changes: 3 additions & 2 deletions src/services/custom-dimensions/customDimensions.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TRACK_EVENT } from '../../constants/track-event.constant';
import { Tracker } from '../../interfaces/tracker';
import { PaqService } from '../paqService/paq.service';

export function setCustomDimensionValue(customDimensionId: string | number, customDimensionValue: string) {
Expand All @@ -9,11 +10,11 @@ export function deleteCustomDimension(customDimensionId: string) {
PaqService.push([TRACK_EVENT.DELETE_CUSTOM_DIMENSION, customDimensionId]);
}

export function getCustomDimensionValue(customDimensionId: string | number): Promise<string> {
export function getCustomDimensionValue(customDimensionId: string | number): Promise<string|undefined> {
return new Promise((resolve, reject) => {
try {
PaqService.push([
function (this: any): void {
function (this: Tracker): void {
resolve(this.getCustomDimensionValue(customDimensionId));
},
]);
Expand Down
6 changes: 4 additions & 2 deletions src/services/custom-events/customEvents.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { TRACK_EVENT } from '../../constants/track-event.constant';
import { Dimensions } from '../../interfaces/utils';
import { PaqService } from '../paqService/paq.service';

export function trackEvent(category: string, action: string, name?: string, value?: number) {
const eventArguments: any[] = [
export function trackEvent(category: string, action: string, name?: string, value?: number, dimensions?:Dimensions) {
const eventArguments = [
category,
action,
...(name ? [name] : []),
...(name ? [value] : []),
...(dimensions ? [dimensions] : []),
];
PaqService.push([TRACK_EVENT.CUSTOM_EVENT, ...eventArguments])
}
13 changes: 8 additions & 5 deletions src/services/dataLayer/dataLayer.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { PiwikProWindow } from '../../interfaces/piwikpro.window';
import { IS_DEBUG } from '../../core';
import { AnyData } from '../../interfaces/utils';

export function push(data: any) {
if (!(window as PiwikProWindow).dataLayer) {
(window as PiwikProWindow).dataLayer = [];

export function push(
data: AnyData
) {
if (!window.dataLayer) {
window.dataLayer = [];
}

IS_DEBUG && console.log('DataLayer push', data);
return (window as PiwikProWindow).dataLayer.push(data);
return window.dataLayer.push(data);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { TRACK_EVENT } from '../../constants/track-event.constant';
import { Tracker } from '../../interfaces/tracker';
import { Dimensions } from '../../interfaces/utils';
import { PaqService } from '../paqService/paq.service';

export function trackLink(url: string, linkType: string, customData?: object, callback?: (params: any) => void) {
export function trackLink(url: string, linkType: string, dimensions?: Dimensions, callback?: () => void) {
PaqService.push([
TRACK_EVENT.LINK,
url,
linkType,
customData,
dimensions,
callback
]);
}
Expand Down Expand Up @@ -39,11 +41,11 @@ export function setLinkTrackingTimer(time: number) {
PaqService.push([TRACK_EVENT.SET_LINK_TRACKING_TIMER, time]);
}

export function getLinkTrackingTimer(): Promise<string> {
export function getLinkTrackingTimer(): Promise<number> {
return new Promise((resolve, reject) => {
try {
PaqService.push([
function (this: any) {
function (this: Tracker) {
resolve(this.getLinkTrackingTimer());
},
]);
Expand Down
3 changes: 2 additions & 1 deletion src/services/e-commerce/e-commerce.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { TRACK_EVENT } from '../../constants/track-event.constant'
import { PaqService } from '../paqService/paq.service'
import { Product } from '../../interfaces/product'
import { PaymentInformation } from '../../interfaces/payment'
import { Tracker } from '../../interfaces/tracker'

/**
* @deprecated since version 1.3.1. Please use the ecommerceAddToCart instead.
Expand Down Expand Up @@ -44,7 +45,7 @@ export function getEcommerceItems(): Promise<object> {
return new Promise((resolve, reject) => {
try {
PaqService.push([
function (this: any): void {
function (this: Tracker): void {
resolve(this.getEcommerceItems())
}
])
Expand Down
4 changes: 3 additions & 1 deletion src/services/goal-conversions/goal-conversions.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { TRACK_EVENT } from '../../constants/track-event.constant';
import { Dimensions } from '../../interfaces/utils';
import { PaqService } from '../paqService/paq.service';

export function trackGoal(goalId: number | string, conversionValue: number, dimensions?: Object) {

export function trackGoal(goalId: number | string, conversionValue: number, dimensions?: Dimensions) {
PaqService.push([
TRACK_EVENT.GOAL,
goalId,
Expand Down
21 changes: 12 additions & 9 deletions src/services/paqService/paq.service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { PiwikProWindow } from '../../interfaces/piwikpro.window';
import { TRACK_EVENT } from '../../constants/track-event.constant';
import { IS_DEBUG } from '../../core';
import { QueueItem } from '../../interfaces/utils';

function push(collection: any[]) {
if (!(window as PiwikProWindow)._paq) {
(window as PiwikProWindow)._paq = [];


function push(collection: QueueItem) {
if (!window._paq) {
window._paq = [];
}

(window as PiwikProWindow)._paq.push(["setCustomUrl", window.location.href]);
(window as PiwikProWindow)._paq.push(["setDocumentTitle", document.title]);
window._paq.push([TRACK_EVENT.SET_CUSTOM_URL, window.location.href]);
window._paq.push([TRACK_EVENT.SET_DOCUMENT_TITLE, document.title]);
IS_DEBUG && console.log('Push', collection);
return (window as PiwikProWindow)._paq.push(collection);
return window._paq.push(collection);
}

export const PaqService = {
push,
}
push
};
3 changes: 2 additions & 1 deletion src/services/site-search/site-search.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { TRACK_EVENT } from '../../constants/track-event.constant';
import { Dimensions } from '../../interfaces/utils';
import { PaqService } from '../paqService/paq.service';

export function trackSiteSearch(keyword: string, category?: string, searchCount?: number, dimensions?: Object) {
export function trackSiteSearch(keyword: string, category?: string, searchCount?: number, dimensions?: Dimensions) {
PaqService.push([
TRACK_EVENT.SEARCH,
keyword,
Expand Down
13 changes: 8 additions & 5 deletions src/services/user-management/userManagement.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { TRACK_EVENT } from '../../constants/track-event.constant';
import { Tracker } from '../../interfaces/tracker';
import { VisitorInfo } from '../../interfaces/visitorInfo';
import { PaqService } from '../paqService/paq.service';

export function getUserId(): Promise<string> {
return new Promise((resolve, reject) => {
try {
PaqService.push([
function (this: any): void {
function (this:Tracker): void {
resolve(this.getUserId());
},
]);
Expand All @@ -25,11 +27,11 @@ export function resetUserId(): void {
PaqService.push([TRACK_EVENT.RESET_USER_ID]);
}

export function getVisitorId(): Promise<any> {
export function getVisitorId(): Promise<string> {
return new Promise((resolve, reject) => {
try {
PaqService.push([
function (this: any): void {
function (this: Tracker): void {
resolve(this.getVisitorId());
},
]);
Expand All @@ -41,11 +43,12 @@ export function getVisitorId(): Promise<any> {
});
}

export function getVisitorInfo(): Promise<any[]> {

export function getVisitorInfo(): Promise<VisitorInfo> {
return new Promise((resolve, reject) => {
try {
PaqService.push([
function (this: any): void {
function (this: Tracker): void {
resolve(this.getVisitorInfo());
},
]);
Expand Down
Loading

0 comments on commit ab25dd2

Please sign in to comment.