Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions build/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as ackeeTracker from "ackee-tracker";
export declare var ackeeInstance: ackeeTracker.AckeeInstance | undefined;
/**
* Use Ackee in React.
* Creates an instance once and a new record every time the pathname changes.
* Safely no-ops during server-side rendering.
* @param {String} pathname - Current path.
* @param {Object} environment - Object containing the URL of the Ackee server and the domain id.
* @param {ackeeTracker.TrackingOptions} options - Ackee options.
*/
export declare const useAckee: (pathname: string, environment: {
server: string;
domainId: string;
}, options?: ackeeTracker.TrackingOptions) => void;
export default useAckee;
42 changes: 42 additions & 0 deletions build/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions build/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 17 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"Tobias Reich <tobias@electerious.com>"
],
"description": "Use Ackee in React",
"main": "src/index.js",
"keywords": [
"react",
"hooks",
Expand All @@ -22,32 +21,43 @@
"url": "https://github.com/electerious/use-ackee.git"
},
"files": [
"src"
"build"
],
"scripts": {
"coveralls": "nyc report --reporter=lcov",
"test": "npm run lint && nyc node_modules/mocha/bin/_mocha",
"lint": "eslint \"{src,test}/**/*.js\""
"test": "npm run build && nyc node_modules/mocha/bin/_mocha",
"lint": "eslint \"{build,test}/**/*.js\"",
"build": "tsc"
},
"dependencies": {
"ackee-tracker": "^5.0.1"
},
"devDependencies": {
"@electerious/eslint-config": "^3.0.0",
"@types/ackee-tracker": "^5.0.2",
"@types/node": "^18.11.0",
"@types/react": "^18.0.21",
"chai": "^4.3.0",
"coveralls": "^3.1.1",
"mocha": "^9.1.3",
"nyc": "^15.1.0",
"react": "^17.0.1"
"react": "^18.1.0",
"typescript": "^4.8.4"
},
"peerDependencies": {
"react": "^17.0.0"
"react": "^18.1.0"
},
"eslintConfig": {
"root": true,
"extends": "@electerious/eslint-config"
},
"eslintIgnore": [
"demos"
]
],
"type": "module",
"exports": "./build/index.js",
"engines": {
"node": ">=14.16"
},
"main": "./build/index.js"
}
50 changes: 0 additions & 50 deletions src/index.js

This file was deleted.

57 changes: 57 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { useMemo, useEffect } from "react";
import * as ackeeTracker from "ackee-tracker";

const isBrowser = typeof window !== "undefined";

export var ackeeInstance: ackeeTracker.AckeeInstance | undefined;

/**
* Use Ackee in React.
* Creates an instance once and a new record every time the pathname changes.
* Safely no-ops during server-side rendering.
* @param {String} pathname - Current path.
* @param {Object} environment - Object containing the URL of the Ackee server and the domain id.
* @param {ackeeTracker.TrackingOptions} options - Ackee options.
*/
export const useAckee = (
pathname: string,
environment: { server: string; domainId: string },
options: ackeeTracker.TrackingOptions = {}
) => {
ackeeInstance = useMemo(() => {
if (isBrowser === false) return;

return ackeeTracker.create(environment.server, options);
}, [
environment.server,
options.detailed,
options.ignoreLocalhost,
options.ignoreOwnVisits,
]);

useEffect(() => {
if (!ackeeInstance) {
console.warn(
"Skipped record creation because useAckee has been called in a non-browser environment"
);
return;
}

if (!pathname.length) {
console.warn(
"Skipped record creation because useAckee has been called without pathname"
);
return;
}

const attributes = ackeeTracker.attributes(options.detailed);
const url = new URL(pathname, location.toString());

return ackeeInstance.record(environment.domainId, {
...attributes,
siteLocation: url.href,
}).stop;
}, [ackeeInstance, pathname, environment.domainId]);
};

export default useAckee;
16 changes: 9 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict'
import { assert, expect } from 'chai';
import { useAckee, ackeeInstance } from './../build/index.js';
import * as ackeeTracker from "ackee-tracker";

const assert = require('chai').assert
const index = require('./../src/index')

describe('index', function() {
it('should be a function', function() {
assert.isFunction(index)
describe('index', function () {
it('should be a function', function () {
assert.isFunction(useAckee)
})
it('ackeeInstance should be ackeeTracker.ackeeInstance type', () => {
expect(ackeeInstance).to.be.a(typeof ackeeTracker.ackeeInstance)
})
})
23 changes: 23 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"outDir": "build",
"module": "ES2022",
"target": "ES2022",
"lib": ["ES2022", "DOM"],
"moduleResolution": "node",
"jsx": "preserve",
"allowJs": true,
"strict": true,
"declaration": true,
"sourceMap": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"esModuleInterop": true,
"noImplicitReturns": true,
"noImplicitAny": true,
"strictNullChecks": true,
"isolatedModules": true
},
"include": ["src"],
"exclude": ["node_modules"]
}