Skip to content
Snippets Groups Projects

日経ビジネスの PushTracker の js ファイルを追加

Merged Ayumi Ishii requested to merge feature/nikkei-business-pt into master
Files
12
+ 125
0
'use strict';
import { storageFunctions, isEqual } from "@saasshare/client-js";
import { bpTrackVars, getToday } from "../share/site";
import constants from "./constants";
import { updatePtAttribute } from "./pt";
const { fromStorage, setStorage } = storageFunctions(constants.storagePrefixKey);
// ローカルの属性を保存する localStorage のキー
const userStatusStorageKey = "user_status";
const lastVisitedAtStorageKey = "last_visited_at";
// ローカルの属性の更新時刻の suffix
const localUpdatedAtKeySuffix = '_updated_at';
/**
* ローカルの属性を更新する
*/
function updateLocalAttributes() {
updateLocalUserStatus();
updateLocalLastVistedAt();
}
/**
* 更新があった属性をリモートに送る
*/
function updateRemoteAttributes(attributeKeys, context) {
const lastUpdatedAt = remoteLastUpdatedAt();
updateRemoteUserStatus(attributeKeys.userStatus, lastUpdatedAt, context);
updateRemoteLastVisitedAt(attributeKeys.lastVisitedAt, lastUpdatedAt, context);
updateRemoteLastUpdatedAt();
}
// ローカルのユーザステータスを更新
function updateLocalUserStatus() {
try {
const newStatus = bpTrackVars().userStatus;
if (!isEqual(getLocalValue(userStatusStorageKey), newStatus)) {
// 前回から変更があれば更新時刻とともに保存
setLocalValue(userStatusStorageKey, newStatus);
setLocalUpdatedAt(userStatusStorageKey);
}
} catch (error) {
console.warn(error);
}
}
// ローカルの最終訪問日を更新
function updateLocalLastVistedAt() {
try {
const today = getToday();
const stored = new Date(getLocalValue(lastVisitedAtStorageKey, 0));
if (!isEqual(stored, today)) {
// 前回から変更があれば更新時刻とともに保存
setLocalValue(lastVisitedAtStorageKey, today);
setLocalUpdatedAt(lastVisitedAtStorageKey);
}
} catch (error) {
console.warn(error);
}
}
// リモートのユーザステータスを更新
function updateRemoteUserStatus(attributeKey, remoteLastUpdatedAt, context) {
try {
// 前回のリモートへの更新以降ローカルの更新があればリモートを更新する
const localUpdatedAt = getLocalUpdatedAt(userStatusStorageKey);
if (localUpdatedAt > remoteLastUpdatedAt) {
const value = fromStorage(userStatusStorageKey);
updatePtAttribute(attributeKey, value, context);
}
} catch (error) {
console.warn(error);
}
}
// リモートの最終訪問日を更新
function updateRemoteLastVisitedAt(attributeKey, remoteLastUpdatedAt, context) {
try {
// 前回のリモートへの更新以降ローカルの更新があればリモートを更新する
const localUpdatedAt = getLocalUpdatedAt(lastVisitedAtStorageKey);
if (localUpdatedAt > remoteLastUpdatedAt) {
const value = fromStorage(lastVisitedAtStorageKey);
updatePtAttribute(attributeKey, value, context);
}
} catch (error) {
console.warn(error);
}
}
const ptLastUpdatedAtKey = 'pt_last_updated_at';
// 最後にリモートの PT の属性更新した日時
function remoteLastUpdatedAt() {
// 前回の記録がなければエポックタイムで返す
return new Date(fromStorage(ptLastUpdatedAtKey, 0));
}
// 最後にリモートの PT の属性更新した日時を更新
function updateRemoteLastUpdatedAt(now = new Date()) {
setStorage(ptLastUpdatedAtKey, now);
}
function getLocalValue(key, orDefault) {
return fromStorage(key, orDefault);
}
function setLocalValue(key, value) {
setStorage(key, value);
}
function getLocalUpdatedAt(key) {
const storageKey = key + localUpdatedAtKeySuffix;
return new Date(fromStorage(storageKey, 0));
}
function setLocalUpdatedAt(key) {
const storageKey = key + localUpdatedAtKeySuffix;
setStorage(storageKey, new Date());
}
export { updateLocalAttributes, updateRemoteAttributes }