File

src/lib/telemetry-utils.service.ts

Index

Properties
Methods

Constructor

constructor(discussionEvents: DiscussionEventsService, router: Router)
Parameters :
Name Type Optional
discussionEvents DiscussionEventsService No
router Router No

Methods

deleteContext
deleteContext(prevTopic)
Parameters :
Name Optional
prevTopic No
Returns : void
getContext
getContext()
Returns : {}
getRollUp
getRollUp()
Returns : {}
logImpression
logImpression(pageId)
Parameters :
Name Optional
pageId No
Returns : void
logInteract
logInteract(event, pageId)
Parameters :
Name Optional
event No
pageId No
Returns : void
setContext
setContext(context)
Parameters :
Name Optional
context No
Returns : void
uppendContext
uppendContext(data)
Parameters :
Name Optional
data No
Returns : void

Properties

_context
Type : []
Default value : []
currentObj
Type : object
Default value : {}
import { DiscussionEventsService } from './discussion-events.service';
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
/* tslint:disable */
import * as _ from 'lodash'
/* tslint:enable */
interface ITelemetryObj {
  eid: string,
  edata: {},
  context?: {}
}

@Injectable({
  providedIn: 'root'
})
export class TelemetryUtilsService {

  _context = []
  currentObj = {};

  constructor(
    private discussionEvents: DiscussionEventsService,
    private router: Router
  ) { }

  setContext(context) {
    this._context = context;
    this.currentObj = _.last(context);
  }

  uppendContext(data) {
    const matchedC = _.find(this._context, { id: data.id });
    if (!_.isEmpty(data) && !_.isEqual(data, matchedC)) {
      this._context.push(data);
    }
    this.currentObj = _.last(this._context);
  }

  deleteContext(prevTopic) {
    const topic = _.find(this._context, prevTopic);
    if (topic) {
      this._context = _.reject(this._context, topic);
    }
  }

  getContext() {
    return this._context;
  }

  logImpression(pageId) {
    this.discussionEvents.emitTelemetry({});
    const impressionEvent: ITelemetryObj = {
      eid: 'IMPRESSION',
      edata: {
        type: 'view',
        pageid: pageId,
        uri: this.router.url
      }
    }
    if (this.currentObj) {
      impressionEvent.context = { cdata: [{
        id: _.get(this.currentObj, 'id').toString(),
        type: _.get(this.currentObj, 'type') }
      ]};
    }
    this.discussionEvents.emitTelemetry(impressionEvent);
  }

  logInteract(event, pageId) {
    const target = _.get(event, 'currentTarget.attributes.id') ||  _.get(event, 'target.attributes.id') ||
     _.get(event, 'srcElement.attributes.id');
    const interactEvent: ITelemetryObj = {
      eid: 'INTERACT',
      edata: {
        id: _.get(target, 'value') || _.get(event, 'action'),
        type: 'CLICK',
        pageid: pageId
      }
    };

    if (this.currentObj) {
      const object = {
        id: _.get(this.currentObj, 'id').toString(),
        type: _.get(this.currentObj, 'type'),
        ver: '1'
      };
      object['rollup'] = this._context.length > 1 ?  this.getRollUp() : {};
      interactEvent.context = {
        cdata: [{
          id: _.get(this.currentObj, 'id').toString(),
          type: _.get(this.currentObj, 'type')
        }], object
      };
    }

    this.discussionEvents.emitTelemetry(interactEvent);
  }

  getRollUp() {

      const rollUp = {};
      const data = _.reject(this._context, this.currentObj);

      if (this._context.length > 1) {
        data.forEach((element, index) => {
          console.log('rollup', element);
          rollUp['l' + (index + 1)] = element.toString();
        });
      }

      if (_.get(this.currentObj, 'type') !== 'Post') {
        return rollUp;
      }

      return {};

  }

}

results matching ""

    No results matching ""