Skip to content

Commit

Permalink
Tracked click events
Browse files Browse the repository at this point in the history
* adding tracked click events

* move customId reference creation into constructor
  • Loading branch information
MetaverseUnknower committed Nov 20, 2022
1 parent 7c9ef5f commit d20ca93
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/classes/StoredImage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { movePlayerTo } from "@decentraland/RestrictedActions";
import { recordEvent } from "../analytics";
import { sdkImageFlippedDimension, sdkImagesAreFlipped, sdkImagesFace, vlmImagesFace } from "../helpers/defaults";
import { getEntityByName } from "../helpers/entity";
import { IEmission, ITexture, ITransform } from "../interfaces/index";
Expand Down Expand Up @@ -139,9 +140,6 @@ export class StoredImageMaterial extends Material implements ITexture, IEmission
createInstance: CallableFunction = (_config: TImageInstanceConfig) => {
this.instanceIds.push(_config.id);
new StoredImageInstance(this, _config);
if (_config.customId) {
imageInstances[_config.customId] = imageInstances[_config.id];
}
imageInstances[_config.id].add();
};

Expand Down Expand Up @@ -194,6 +192,10 @@ export class StoredImageInstance extends StoredEntityInstance implements ITransf
} else if (this.show) {
this.add();
}

if (this.customId) {
imageInstances[this.customId] = imageInstances[this.id];
}
}

add: CallableFunction = () => {
Expand Down Expand Up @@ -239,8 +241,8 @@ export class StoredImageInstance extends StoredEntityInstance implements ITransf
new Transform({
position: new Vector3(position.x, position.y, position.z),
scale: new Vector3(scale.x, scale.y, scale.z),
rotation: Quaternion.Euler(rotation.x, rotation.y, rotation.z)
})
rotation: Quaternion.Euler(rotation.x, rotation.y, rotation.z),
}),
);
};

Expand All @@ -267,6 +269,14 @@ export class StoredImageInstance extends StoredEntityInstance implements ITransf
this.modifiedTransform.rotation.y += imageRotationDegree;
};

trackClickEvent: CallableFunction = (clickEvent: TClickEvent, id: string) => {
const trackingId = clickEvent.trackingId || id;

if (clickEvent.hasTracking) {
recordEvent(trackingId);
}
};

updateClickEvent: CallableFunction = (clickEvent: TClickEvent) => {
if (!clickEvent) {
return;
Expand All @@ -291,8 +301,9 @@ export class StoredImageInstance extends StoredEntityInstance implements ITransf
pointerDownEvent = new OnPointerDown(
() => {
openExternalURL(clickEvent.externalLink);
this.trackClickEvent(clickEvent, `click-event-(external-link)-${this.customId || this.id}`);
},
{ showFeedback, hoverText }
{ showFeedback, hoverText },
);
break;
case EClickEventType.SOUND: //play a sound
Expand All @@ -301,24 +312,27 @@ export class StoredImageInstance extends StoredEntityInstance implements ITransf
pointerDownEvent = new OnPointerDown(
() => {
source.playOnce();
this.trackClickEvent(clickEvent, `click-event-(sound)-${this.customId || this.id}`);
},
{ showFeedback, hoverText }
{ showFeedback, hoverText },
);
break;
case EClickEventType.MOVE: // move player
pointerDownEvent = new OnPointerDown(
() => {
movePlayerTo(clickEvent.moveTo.position, clickEvent.moveTo.cameraTarget);
this.trackClickEvent(clickEvent, `click-event-(move-player)-${this.customId || this.id}`);
},
{ showFeedback, hoverText }
{ showFeedback, hoverText },
);
break;
case EClickEventType.TELEPORT: // teleport player
pointerDownEvent = new OnPointerDown(
() => {
teleportTo(clickEvent.teleportTo);
this.trackClickEvent(clickEvent, `click-event-(teleport-player)-${this.customId || this.id}`);
},
{ showFeedback, hoverText }
{ showFeedback, hoverText },
);
break;
}
Expand Down
2 changes: 2 additions & 0 deletions src/types/ClickEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ export type TClickEvent = {
sound?: string;
moveTo?: { cameraTarget: TTransform; position: TTransform };
teleportTo?: string;
hasTracking?: boolean;
trackingId?: string;
};

0 comments on commit d20ca93

Please sign in to comment.