Detalhes do pacote

impression-tracker-react-hook

promotedai4.7kMIT8.3.2

Tracks impressions in React using a Hook

react, library, impression, tracking

readme (leia-me)

impression-tracker-react-hook

This library is used to track impressions using a react useImpressionTracker hook.

See unit tests for a detailed example for both the React Hook and Higher Order Component (HOC).

Hook

import { useImpressionTracker } from 'impression-tracker-react-hook';
import { createEventLogger } from 'promoted-snowplow-logger';

export const handleError = process.env.NODE_ENV !== 'production' ? (err) => { throw err; } : console.error;

export const eventLogger = createEventLogger({
  enabled: true,
  handleError,
});

const HookedExampleComponent = ({
  // Set insertionId and/or contentId.
  insertionId,
  contentId,
}: Props) => {
  // ref needs to be set on the div to observe.
  // impressionId can be passed directly into a logAction call.
  // logImpressionFunctor can be called to force an impression.
  const [ref, impressionId, logImpressionFunctor] = useImpressionTracker({
    enable: true,
    insertionId,
    contentId,
    handleError,
    logImpression: eventLogger.logImpression,
  });
  return <div ref={ref}>{text}</div>;
};

Higher-Order Components (HOC)

interface Props {
  ...
  // TODO - set this ref on the div.
  impressionRef: (node?: Element | null) => void;
  // Optional props.
  impressionId: string;
  // In case you want to log an impression early.
  logImpressionFunctor: () => void;
}

class ExampleComponent extends React.Component<Props> {
  ...
  render() {
    ...
    return <div ref={this.props.impressionRef}>{text}</div>;
  }
}

const WrappedExampleComponent = withImpressionTracker(ExampleComponent, {
  handleError,
  isEnabled: () => impressionLoggingEnabled,
  getContentId: props => props.contentId,
  getInsertionId: props => props.insertionId,
  // Can be changed to modify the impression.
  logImpression: eventLogger.logImpression,
});

Using Compose

const WrappedExampleComponent = compose(
  ...,
  composableImpressionTracker({
    handleError,
    isEnabled: () => impressionLoggingEnabled,
    getContentId: props => props.contentId,
    getInsertionId: props => props.insertionId,
    // Can be changed to modify the impression.
    logImpression: eventLogger.logImpression,
  })
)(ExampleComponent);

Features

Uses

Scripts

  • Run most commands: npm run finish
  • Build the project: npm run build
    • Validate output bundle size with npm run size
  • Lint the project: npm run lint
  • Run unit tests: npm test or npm test

When developing locally

Broken - We previously had an npm run updateLink command to use npm link for local development. This fails with a Error: Cannot find module 'react'.

For now, just copy/paste the impression tracker code into the client code and test it out.

Deploy

We use a GitHub action that runs semantic-release to determine how to update versions. Just do a normal code review and this should work. Depending on the message prefixes (e.g. feat:, fix:, clean:, docs:), it'll update the version appropriately.

When doing a breaking change, add BREAKING CHANGE: to the PR. The colon is important.

Resources

The base of this repository is a combination of the following repos:

changelog (log de mudanças)

8.3.1 (2024-10-07)

Bug Fixes

8.3.0 (2024-07-05)

Features

8.2.0 (2024-06-30)

Features

8.1.2 (2024-06-28)

Bug Fixes

8.1.1 (2024-06-28)

Bug Fixes

8.1.0 (2024-06-28)

Features

  • bump node and dependency versions (#66) (e4413b0)

8.0.0 (2023-09-08)

Features

BREAKING CHANGES

  • renames logUserId to anonUserId.

TESTING: ran unit tests

7.8.0 (2023-04-28)

Features

7.7.0 (2023-04-01)

Features

  • support a default ImpressionSourceType; set to DELIVERY (#61) (08e8e21)

7.6.0 (2022-06-09)

Features

  • support composableImpressionTracker (#57) (512564e)

7.5.0 (2022-06-06)

Features

7.4.0 (2022-06-06)

Features

7.3.0 (2022-06-02)

Features

7.2.0 (2022-06-02)

Features

7.1.0 (2022-06-02)

Features

7.0.1 (2021-09-13)

Bug Fixes

7.0.0 (2021-06-10)

Bug Fixes

  • another PR to force the breaking change (0070b5d)

BREAKING CHANGES

  • I think this line needs a colon in it to work correctly.

TESTING=none

6.1.0 (2021-05-29)

Features

  • support contentId. rename handleLogError. tests (b5e779f)

6.0.0 (2021-03-27)

Features

  • force a breaking change for new protos (7f5138a)

BREAKING CHANGES

  • the last commit switched to using new protos that break the interface

5.1.0 (2021-03-27)

Features

  • switch to new proto schema (8fccfc4)

5.0.2 (2021-01-28)

Bug Fixes

  • how enable==undefined works (4787f6b)

5.0.1 (2021-01-27)

Bug Fixes

  • hide no-insertion-id error when disabled (3c841e5)

5.0.0 (2021-01-27)

Features

  • change hoc's isenabled to take props (46a91c4)

BREAKING CHANGES

  • change the withImpressionTracker HOC

TESTING=manual

4.0.0 (2021-01-27)

Features

  • dict arg, displayname and undefined insertionid (4d4af57)

BREAKING CHANGES

    • changes the HOC interface

TESTING=unit tests

3.0.2 (2021-01-27)

Bug Fixes

3.0.1 (2021-01-13)

Bug Fixes

  • how changing insertion id works (692ad28)

3.0.0 (2021-01-13)

Features

BREAKING CHANGES

  • logImpressionFunctor

TESTING=manual

2.0.0 (2021-01-13)

Features

BREAKING CHANGES

  • this changes the signature on logImpression

TESTING=manual

1.1.0 (2021-01-13)

Features

1.0.19 (2021-01-13)

Bug Fixes

  • change npm release to use dist (87ce8e0)

1.0.18 (2021-01-13)

Bug Fixes

  • try to rollup code to match other repo (cc739f0)

1.0.17 (2021-01-11)

Bug Fixes

  • the push by removing the registry (e467939)

1.0.16 (2021-01-11)

Bug Fixes

  • change scopes and names back to working version (60bec8a)
  • try to fix scope by changing registry (d341ae8)

1.0.15 (2021-01-11)

Bug Fixes

1.0.14 (2021-01-11)

Bug Fixes

1.0.13 (2021-01-11)

Bug Fixes

  • another attempt to fix scope (25a2920)

1.0.12 (2021-01-11)

Bug Fixes

1.0.11 (2021-01-11)

Bug Fixes

  • another attempt to fix scope (4ae8a21)

1.0.10 (2021-01-11)

Bug Fixes

  • try to get package scopes working (99b6482)

1.0.9 (2021-01-11)

Bug Fixes

  • try changing package name (edb82f2)

1.0.8 (2021-01-11)

Bug Fixes

1.0.7 (2021-01-11)

Bug Fixes

  • add release-rc (e6a4c73)
  • small change to push again (0b885f3)
  • small change to push again (2414632)
  • small change to push again (5e25770)
  • small change to push release (75a88c8)
  • small change to trigger push (98f45c6)