snowplow/snowplow-javascript-tracker

View on GitHub
plugins/browser-plugin-youtube-tracking/src/index.ts

Summary

Maintainability
C
1 day
Test Coverage

File index.ts has 268 lines of code (exceeds 250 allowed). Consider refactoring.
Open

/*
 * Copyright (c) 2022 Snowplow Analytics Ltd
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
Severity: Minor
Found in plugins/browser-plugin-youtube-tracking/src/index.ts - About 2 hrs to fix

    Function trackingOptionsParser has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.
    Open

    export function trackingOptionsParser(id: string | YT.Player, conf?: MediaTrackingOptions): TrackingOptions {
      const player = typeof id === 'string' ? undefined : id;
      const elementId = typeof id === 'string' ? id : id.getIframe().id;
      const defaultBoundaries = [10, 25, 50, 75];
      const defaultUpdateRate = 500;
    Severity: Minor
    Found in plugins/browser-plugin-youtube-tracking/src/index.ts - About 2 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function trackingOptionsParser has 41 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

    export function trackingOptionsParser(id: string | YT.Player, conf?: MediaTrackingOptions): TrackingOptions {
      const player = typeof id === 'string' ? undefined : id;
      const elementId = typeof id === 'string' ? id : id.getIframe().id;
      const defaultBoundaries = [10, 25, 50, 75];
      const defaultUpdateRate = 500;
    Severity: Minor
    Found in plugins/browser-plugin-youtube-tracking/src/index.ts - About 1 hr to fix

      Function handleYouTubeIframeAPI has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
      Open

      function handleYouTubeIframeAPI(conf: TrackingOptions) {
        // First we check if the script tag exists in the DOM, and enable the API if not
        const scriptTags = Array.prototype.slice.call(document.getElementsByTagName('script'));
        if (!scriptTags.some((s) => s.src === YouTubeIFrameAPIURL)) {
          // Load the Iframe API
      Severity: Minor
      Found in plugins/browser-plugin-youtube-tracking/src/index.ts - About 55 mins to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function attachListeners has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
      Open

      function attachListeners(player: YT.Player, conf: TrackingOptions) {
        const builtInEvents: Record<string, Function> = {
          [YTPlayerEvent.ONSTATECHANGE]: (e: YT.OnStateChangeEvent) => {
            const stateEventInCaptureEvents = conf.captureEvents.indexOf(YTStateEvent[e.data.toString()]) !== -1;
            // If the user is tracking 'percentprogress' events, we need to pass these state changes to `progressHandler`,
      Severity: Minor
      Found in plugins/browser-plugin-youtube-tracking/src/index.ts - About 35 mins to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function enableYouTubeTracking has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
      Open

      export function enableYouTubeTracking(args: { id: string | YT.Player; options?: MediaTrackingOptions }) {
        if (!Object.keys(_trackers).length) {
          LOG.error('Check YoutubeTrackingPlugin is initialized in tracker config');
          return;
        }
      Severity: Minor
      Found in plugins/browser-plugin-youtube-tracking/src/index.ts - About 35 mins to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Identical blocks of code found in 2 locations. Consider refactoring.
      Open

      function trackEvent(
        event: SelfDescribingJson<MediaPlayerEvent> & CommonEventProperties,
        trackers: Array<string> = Object.keys(_trackers)
      ): void {
        dispatchToTrackersInCollection(trackers, _trackers, (t) => {
      Severity: Minor
      Found in plugins/browser-plugin-youtube-tracking/src/index.ts and 1 other location - About 55 mins to fix
      plugins/browser-plugin-media-tracking/src/index.ts on lines 191..198

      Duplicated Code

      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

      Tuning

      This issue has a mass of 54.

      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

      Refactorings

      Further Reading

      Similar blocks of code found in 2 locations. Consider refactoring.
      Open

        if (!playerInstance.seekTracking.enabled && conf.captureEvents.indexOf(SnowplowEvent.SEEK) !== -1) {
          enableSeekTracking(player, conf, eventData);
        }
      Severity: Minor
      Found in plugins/browser-plugin-youtube-tracking/src/index.ts and 1 other location - About 50 mins to fix
      plugins/browser-plugin-youtube-tracking/src/index.ts on lines 308..310

      Duplicated Code

      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

      Tuning

      This issue has a mass of 52.

      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

      Refactorings

      Further Reading

      Similar blocks of code found in 2 locations. Consider refactoring.
      Open

        if (!playerInstance.volumeTracking.enabled && conf.captureEvents.indexOf(SnowplowEvent.VOLUMECHANGE) !== -1) {
          enableVolumeTracking(player, conf, eventData);
        }
      Severity: Minor
      Found in plugins/browser-plugin-youtube-tracking/src/index.ts and 1 other location - About 50 mins to fix
      plugins/browser-plugin-youtube-tracking/src/index.ts on lines 305..307

      Duplicated Code

      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

      Tuning

      This issue has a mass of 52.

      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

      Refactorings

      Further Reading

      There are no issues that match your filters.

      Category
      Status