MixPanel -Android端埋點(diǎn)技術(shù)研究

目前在app上通過記錄用戶操作(俗稱埋點(diǎn)),來分析用戶行為的做法,已經(jīng)成了app必不可少的一部分。有關(guān)app的埋點(diǎn)技術(shù),也在發(fā)展中。正好最近項(xiàng)目組研發(fā)了一個(gè)埋點(diǎn)的sdk,所以把相關(guān)知識梳理下。

埋點(diǎn)方式

  1. 代碼埋點(diǎn)

    這種方式主要是由程序猿們手動(dòng)在代碼中的回調(diào)事件里加上埋點(diǎn)代碼。優(yōu)點(diǎn)是高度定制,想怎么埋怎么埋,缺點(diǎn)是工作量大,而且易出錯(cuò),難維護(hù)。

  2. 可視化埋點(diǎn)

    這種埋點(diǎn)方式分為兩種,一是使用后臺界面配置需要埋點(diǎn)的位置,app下載配置文件,將需要埋點(diǎn)的事件上傳(代表MixPanel,百度,talkingdata等)。二是app把所有事件上傳,后臺自己選擇需要埋點(diǎn)的點(diǎn)(代表heap)。

兩種埋點(diǎn)方式各有優(yōu)劣,但是由于技術(shù)目前還在發(fā)展中,并沒有形成完全統(tǒng)一的理論以及方式,因此現(xiàn)在大多是這兩種方式并存。

參考文獻(xiàn): http://blog.csdn.net/vshuang/article/details/60361314

MixPanel源碼分析-Android

下面是分析MixPanel的源碼,這應(yīng)該是唯一的開源的商業(yè)埋點(diǎn)實(shí)現(xiàn)(其他我沒找到),提供可視化埋點(diǎn)以及代碼埋點(diǎn)方式。開源地址:https://github.com/mixpanel ,我主要是研究Android的代碼。

本文的分析并不透徹,主要由于mixpanel的代碼比較復(fù)雜,很多是和服務(wù)器的交互,在不了解協(xié)議的情況下,我也是連蒙帶猜來看源碼的。想透徹分析的同學(xué),可以在mixpanel的網(wǎng)站上注冊一個(gè)應(yīng)用,再在應(yīng)用里集成mixpanel的源碼,然后加日志或者debug來分析。由于時(shí)間有限,我并沒有這么做。請見諒。

首先是mixpanel的入口,MixPanelApi

該類中有大量的方法叫做Tweak,這個(gè)是用來做abtest的,在服務(wù)器上做相應(yīng)的配置,客戶端可以拉取配置實(shí)現(xiàn)不同的功能。本文不討論這個(gè)。

主要方法就是track,

/**
 * Track an event.
 *
 * <p>Every call to track eventually results in a data point sent to Mixpanel. These data points
 * are what are measured, counted, and broken down to create your Mixpanel reports. Events
 * have a string name, and an optional set of name/value pairs that describe the properties of
 * that event.
 *
 * @param eventName The name of the event to send
 * @param properties A JSONObject containing the key value pairs of the properties to include in this event.
 *                   Pass null if no extra properties exist.
 */
public void track(String eventName, JSONObject properties) {
    track(eventName, properties, false);
}

我們通過不停跟蹤代碼發(fā)現(xiàn),這個(gè)方法會把埋點(diǎn)的event,生成一個(gè)AnalyticsMessages.EventDescription對象,然后通過handler,發(fā)送到后臺線程中去處理,代碼如下

track(){
  final AnalyticsMessages.EventDescription eventDescription =
    new AnalyticsMessages.EventDescription(eventName, messageProps, mToken, isAutomaticEvent);
  mMessages.eventsMessage(eventDescription);
}
// ...跳轉(zhuǎn)至eventsMessage
public void eventsMessage(final EventDescription eventDescription) {
        final Message m = Message.obtain();
        m.what = ENQUEUE_EVENTS;
        m.obj = eventDescription;
        mWorker.runMessage(m);
}
//消息處理
if (msg.what == ENQUEUE_EVENTS) {
  final EventDescription eventDescription = (EventDescription) msg.obj;
  try {
    //省略部分代碼
    returnCode = mDbAdapter.addJSON(message, token, MPDbAdapter.Table.EVENTS, eventDescription.isAutomatic());
  } catch (final JSONException e) {
    MPLog.e(LOGTAG, "Exception tracking event " + eventDescription.getEventName(), e);
  }
}

可以看到,最終數(shù)據(jù)被存儲到了數(shù)據(jù)庫里,具體的數(shù)據(jù)庫表結(jié)構(gòu)大家可以自行看源碼,我就不研究哦了。

那數(shù)據(jù)什么時(shí)候上傳呢,主要是在activiyt的onPause之后上傳。

@Override
public void onActivityPaused(final Activity activity) {
    mPaused = true;

    if (check != null) {
        mHandler.removeCallbacks(check);
    }

    mHandler.postDelayed(check = new Runnable(){
        @Override
        public void run() {
            if (mIsForeground && mPaused) {
                mIsForeground = false;
                try {
                    double sessionLength = System.currentTimeMillis() - sStartSessionTime;
                    if (sessionLength >= mConfig.getMinimumSessionDuration() && sessionLength < mConfig.getSessionTimeoutDuration()) {
                        DecimalFormat df = new DecimalFormat("#.0");
                        String sessionLengthString = df.format((System.currentTimeMillis() - sStartSessionTime) / 1000);
                        JSONObject sessionProperties = new JSONObject();
                        sessionProperties.put(AutomaticEvents.SESSION_LENGTH, sessionLengthString);
                        mMpInstance.track(AutomaticEvents.SESSION, sessionProperties, true);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                mMpInstance.flush(); //上傳
            }
        }
    }, CHECK_DELAY);
}

用戶也可以通過MixPanelApi的flush方法上傳

public void flush() {
    mMessages.postToServer(new AnalyticsMessages.FlushDescription(mToken));
}

這就是事件埋點(diǎn)的基本流程,當(dāng)然功能不止這些,還可以通過activity的生命周期,記錄頁面停留時(shí)長等,這些都是基于這個(gè)基本流程來處理的。

可視化埋點(diǎn)

? 我覺得埋點(diǎn)主要的難點(diǎn)就是在可視化埋點(diǎn)上,如何做到良好的用戶體驗(yàn)以及性能呢。我們一起來看看MixPanel是怎么做的。

首先看一下官網(wǎng)的介紹 https://mixpanel.com/autotrack/

通過視頻可以看到,網(wǎng)頁后臺可以找到我們所有可以埋點(diǎn)的區(qū)域,該區(qū)域會高亮+邊框顯示出來,點(diǎn)擊該區(qū)域,就會顯示彈出一個(gè)對話框,就可以把這個(gè)區(qū)域射成一個(gè)埋點(diǎn)的位置。

這看起來是不是比代碼埋點(diǎn)好多啦。那服務(wù)器是怎么找到app中可以埋點(diǎn)的位置的呢。我們來看一下源碼,首先是連接上配置界面的地方,是通過websocket連接的,mixpanel繼承了大量websocket的實(shí)現(xiàn),這里我們就不管他了,感興趣的同學(xué)可以去自己研究下websocket的開源實(shí)現(xiàn)。具體處理協(xié)議的地方是EditorClient這個(gè)類

private class EditorClient extends WebSocketClient {
    public EditorClient(URI uri, int connectTimeout, Socket sslSocket) throws InterruptedException {
        super(uri, new Draft_17(), null, connectTimeout);
        setSocket(sslSocket);
    }

    @Override
    public void onOpen(ServerHandshake handshakedata) {
        MPLog.v(LOGTAG, "Websocket connected");
    }

    @Override
    public void onMessage(String message) {
        MPLog.v(LOGTAG, "Received message from editor:\n" + message);
        try {
            final JSONObject messageJson = new JSONObject(message);
            final String type = messageJson.getString("type");
            if (type.equals("device_info_request")) {
                mService.sendDeviceInfo();
            } else if (type.equals("snapshot_request")) {
                mService.sendSnapshot(messageJson);
            } else if (type.equals("change_request")) {
                mService.performEdit(messageJson);
            } else if (type.equals("event_binding_request")) {
                mService.bindEvents(messageJson);
            } else if (type.equals("clear_request")) {
                mService.clearEdits(messageJson);
            } else if (type.equals("tweak_request")) {
                mService.setTweaks(messageJson);
            }
        } catch (final JSONException e) {
            MPLog.e(LOGTAG, "Bad JSON received:" + message, e);
        }
    }

可以看到OnMessage的地方有這么幾個(gè)接口。這些都是后臺web頁面發(fā)過來的消息,然后app端執(zhí)行相應(yīng)的操作。

device_info_request 這個(gè)就不說了,顯然是獲取一些設(shè)備的信息。

snapshot_request 這個(gè)就是關(guān)鍵的地方,這里是app端將當(dāng)前展示的頁面的截圖,發(fā)送給后端,這樣后端就可以顯示出來了。我們通過代碼跟蹤,找到了實(shí)現(xiàn)在ViewCrawler里的sendSnapshot方法。

private void sendSnapshot(JSONObject message) {
    final long startSnapshot = System.currentTimeMillis();
    //...省略
    try {
        writer.write("{");
        writer.write("\"type\": \"snapshot_response\",");
        writer.write("\"payload\": {");
        {
            writer.write("\"activities\":");
            writer.flush();
            mSnapshot.snapshots(mEditState, out);
        }

        final long snapshotTime = System.currentTimeMillis() - startSnapshot;
        writer.write(",\"snapshot_time_millis\": ");
        writer.write(Long.toString(snapshotTime));

        writer.write("}"); // } payload
        writer.write("}"); // } whole message
    } catch (final IOException e) {
        MPLog.e(LOGTAG, "Can't write snapshot request to server", e);
    } finally {
        try {
            writer.close();
        } catch (final IOException e) {
            MPLog.e(LOGTAG, "Can't close writer.", e);
        }
    }
}

關(guān)鍵代碼在ViewSnapShot里

/**
 * Take a snapshot of each activity in liveActivities. The given UIThreadSet will be accessed
 * on the main UI thread, and should contain a set with elements for every activity to be
 * snapshotted. Given stream out will be written on the calling thread.
 */
public void snapshots(UIThreadSet<Activity> liveActivities, OutputStream out) throws IOException {
    mRootViewFinder.findInActivities(liveActivities);
    final FutureTask<List<RootViewInfo>> infoFuture = new FutureTask<List<RootViewInfo>>(mRootViewFinder);
    mMainThreadHandler.post(infoFuture);

    final OutputStreamWriter writer = new OutputStreamWriter(out);
    List<RootViewInfo> infoList = Collections.<RootViewInfo>emptyList();
    writer.write("[");

    try {
        infoList = infoFuture.get(1, TimeUnit.SECONDS);
    } catch (final InterruptedException e) {
        MPLog.d(LOGTAG, "Screenshot interrupted, no screenshot will be sent.", e);
    } catch (final TimeoutException e) {
        MPLog.i(LOGTAG, "Screenshot took more than 1 second to be scheduled and executed. No screenshot will be sent.", e);
    } catch (final ExecutionException e) {
        MPLog.e(LOGTAG, "Exception thrown during screenshot attempt", e);
    }

RootViewInfo是一個(gè)Future,主要的方法是taskSnapShot

private void takeScreenshot(final RootViewInfo info) {
    final View rootView = info.rootView;
    Bitmap rawBitmap = null;

    try {
        final Method createSnapshot = View.class.getDeclaredMethod("createSnapshot", Bitmap.Config.class, Integer.TYPE, Boolean.TYPE);
        createSnapshot.setAccessible(true);
        rawBitmap = (Bitmap) createSnapshot.invoke(rootView, Bitmap.Config.RGB_565, Color.WHITE, false);
    } catch (final NoSuchMethodException e) {
        MPLog.v(LOGTAG, "Can't call createSnapshot, will use drawCache", e);
    } catch (final IllegalArgumentException e) {
        MPLog.d(LOGTAG, "Can't call createSnapshot with arguments", e);
    } catch (final InvocationTargetException e) {
        MPLog.e(LOGTAG, "Exception when calling createSnapshot", e);
    } catch (final IllegalAccessException e) {
        MPLog.e(LOGTAG, "Can't access createSnapshot, using drawCache", e);
    } catch (final ClassCastException e) {
        MPLog.e(LOGTAG, "createSnapshot didn't return a bitmap?", e);
    }

    Boolean originalCacheState = null;
    try {
        if (null == rawBitmap) {
            originalCacheState = rootView.isDrawingCacheEnabled();
            rootView.setDrawingCacheEnabled(true);
            rootView.buildDrawingCache(true);
            rawBitmap = rootView.getDrawingCache();
        }
    } catch (final RuntimeException e) {
        MPLog.v(LOGTAG, "Can't take a bitmap snapshot of view " + rootView + ", skipping for now.", e);
    }

    float scale = 1.0f;
    if (null != rawBitmap) {
        final int rawDensity = rawBitmap.getDensity();

        if (rawDensity != Bitmap.DENSITY_NONE) {
            scale = ((float) mClientDensity) / rawDensity;
        }

        final int rawWidth = rawBitmap.getWidth();
        final int rawHeight = rawBitmap.getHeight();
        final int destWidth = (int) ((rawBitmap.getWidth() * scale) + 0.5);
        final int destHeight = (int) ((rawBitmap.getHeight() * scale) + 0.5);

        if (rawWidth > 0 && rawHeight > 0 && destWidth > 0 && destHeight > 0) {
            mCachedBitmap.recreate(destWidth, destHeight, mClientDensity, rawBitmap);
        }
    }

    if (null != originalCacheState && !originalCacheState) {
        rootView.setDrawingCacheEnabled(false);
    }
    info.scale = scale;
    info.screenshot = mCachedBitmap;
}

這里就知道了,首先通過反射view的createSnapshot方法,嘗試獲取view的截圖,如果沒有成功,則調(diào)用截屏的api,來獲取Drawingcache。獲取到之后,根據(jù)當(dāng)前手機(jī)屏幕的分辨率來縮放一次。保證跟手機(jī)的分辨率一致。再上傳給服務(wù)器,當(dāng)然這里只是屏幕的截圖,只根據(jù)截圖,是無法知道控件點(diǎn)擊位置的。然后又做了什么呢,我們繼續(xù)看代碼:

private void snapshotView(JsonWriter j, View view)
            throws IOException {
        final int viewId = view.getId();
        final String viewIdName;
        if (-1 == viewId) {
            viewIdName = null;
        } else {
            viewIdName = mResourceIds.nameForId(viewId);
        }
        j.beginObject();
        j.name("hashCode").value(view.hashCode());
        j.name("id").value(viewId);
        j.name("mp_id_name").value(viewIdName);

        final CharSequence description = view.getContentDescription();
        if (null == description) {
            j.name("contentDescription").nullValue();
        } else {
            j.name("contentDescription").value(description.toString());
        }

        final Object tag = view.getTag();
        if (null == tag) {
            j.name("tag").nullValue();
        } else if (tag instanceof CharSequence) {
            j.name("tag").value(tag.toString());
        }

        j.name("top").value(view.getTop());
        j.name("left").value(view.getLeft());
        j.name("width").value(view.getWidth());
        j.name("height").value(view.getHeight());
        j.name("scrollX").value(view.getScrollX());
        j.name("scrollY").value(view.getScrollY());
        j.name("visibility").value(view.getVisibility());

        float translationX = 0;
        float translationY = 0;
        if (Build.VERSION.SDK_INT >= 11) {
            translationX = view.getTranslationX();
            translationY = view.getTranslationY();
        }

        j.name("translationX").value(translationX);
        j.name("translationY").value(translationY);

        j.name("classes");
        j.beginArray();
       // ..省略部分
        if (view instanceof ViewGroup) {
            final ViewGroup group = (ViewGroup) view;
            final int childCount = group.getChildCount();
            for (int i = 0; i < childCount; i++) {
                final View child = group.getChildAt(i);
                // child can be null when views are getting disposed.
                if (null != child) {
                    snapshotView(j, child);
                }
            }
        }
    }

這里就是關(guān)鍵,通過遍歷當(dāng)前view,將所有的view的信息,都傳給了后端,尤其是top,left,width,height這些信息,通過這些信息,后端就可以確定view的位置。這里我覺得是有優(yōu)化空間的,完全可以只上傳可以被埋點(diǎn)的view,例如button等,像一些純展示的無法點(diǎn)擊的view,其實(shí)沒必要上傳的。例如大部分textview。這樣增加了后端的負(fù)擔(dān)。

當(dāng)在后臺操作,選擇了一個(gè)點(diǎn)擊的view之后,app端就會收到event_binding_request消息,我們來看看如何處理這個(gè)消息的

final int size = mEditorEventBindings.size();
for (int i = 0; i < size; i++) {
    final Pair<String, JSONObject> changeInfo = mEditorEventBindings.get(i);
    try {
        final ViewVisitor visitor = mProtocol.readEventBinding(changeInfo.second, mDynamicEventTracker);
        newVisitors.add(new Pair<String, ViewVisitor>(changeInfo.first, visitor));
    } catch (final EditProtocol.InapplicableInstructionsException e) {
        MPLog.i(LOGTAG, e.getMessage());
    } catch (final EditProtocol.BadInstructionsException e) {
        MPLog.e(LOGTAG, "Bad editor event binding cannot be applied.", e);
    }
}

首先調(diào)用readEventBinding讀取服務(wù)端發(fā)來的信息,然后將它存在一個(gè)ViewVisitor里,想找到具體的view,則需要識別view的一些基本的特征,之前的代碼已經(jīng)看到了,view的id,tag,contentdescription等特征,都已經(jīng)被上傳至服務(wù)器,這時(shí)服務(wù)器又會把它下發(fā)回來,放在path參數(shù)里。

// Package access FOR TESTING ONLY
/* package */ List<Pathfinder.PathElement> readPath(JSONArray pathDesc, ResourceIds idNameToId) throws JSONException {
    final List<Pathfinder.PathElement> path = new ArrayList<Pathfinder.PathElement>();

    for (int i = 0; i < pathDesc.length(); i++) {
        final JSONObject targetView = pathDesc.getJSONObject(i);

        final String prefixCode = JSONUtils.optionalStringKey(targetView, "prefix");
        final String targetViewClass = JSONUtils.optionalStringKey(targetView, "view_class");
        final int targetIndex = targetView.optInt("index", -1);
        final String targetDescription = JSONUtils.optionalStringKey(targetView, "contentDescription");
        final int targetExplicitId = targetView.optInt("id", -1);
        final String targetIdName = JSONUtils.optionalStringKey(targetView, "mp_id_name");
        final String targetTag = JSONUtils.optionalStringKey(targetView, "tag");

        final int prefix;
        if ("shortest".equals(prefixCode)) {
            prefix = Pathfinder.PathElement.SHORTEST_PREFIX;
        } else if (null == prefixCode) {
            prefix = Pathfinder.PathElement.ZERO_LENGTH_PREFIX;
        } else {
            MPLog.w(LOGTAG, "Unrecognized prefix type \"" + prefixCode + "\". No views will be matched");
            return NEVER_MATCH_PATH;
        }

        final int targetId;

        final Integer targetIdOrNull = reconcileIds(targetExplicitId, targetIdName, idNameToId);
        if (null == targetIdOrNull) {
            return NEVER_MATCH_PATH;
        } else {
            targetId = targetIdOrNull.intValue();
        }

        path.add(new Pathfinder.PathElement(prefix, targetViewClass, targetIndex, targetId, targetDescription, targetTag));
    }

    return path;
}

通過這些參數(shù),就可以找個(gè)那個(gè)view,繼而對view進(jìn)行監(jiān)聽,監(jiān)聽的方式就比較簡單了,用谷歌提供的Accessibility相關(guān)api就可以做到。

if ("click".equals(eventType)) {
    return new ViewVisitor.AddAccessibilityEventVisitor(
        path,
        AccessibilityEvent.TYPE_VIEW_CLICKED,
        eventName,
        listener
    );
} else if ("selected".equals(eventType)) {
    return new ViewVisitor.AddAccessibilityEventVisitor(
        path,
        AccessibilityEvent.TYPE_VIEW_SELECTED,
        eventName,
        listener
    );
} else if ("text_changed".equals(eventType)) {
    return new ViewVisitor.AddTextChangeListener(path, eventName, listener);
} else if ("detected".equals(eventType)) {
    return new ViewVisitor.ViewDetectorVisitor(path, eventName, listener);
} else {
    throw new BadInstructionsException("Mixpanel can't track event type \"" + eventType + "\"");
}

而匹配view的規(guī)則也很簡單,就是對比class,id,contentDescription,tag四個(gè)元素

private boolean matches(PathElement matchElement, View subject) {
    if (null != matchElement.viewClassName &&
            !hasClassName(subject, matchElement.viewClassName)) {
        return false;
    }

    if (-1 != matchElement.viewId && subject.getId() != matchElement.viewId) {
        return false;
    }

    if (null != matchElement.contentDescription &&
            !matchElement.contentDescription.equals(subject.getContentDescription())) {
        return false;
    }

    final String matchTag = matchElement.tag;
    if (null != matchElement.tag) {
        final Object subjectTag = subject.getTag();
        if (null == subjectTag || !matchTag.equals(subject.getTag().toString())) {
            return false;
        }
    }

    return true;
}

這樣,在每次app的activity的onresume方法里,都會去做這個(gè)尋找匹配的view的過程,具體看viewcrawler的onActivityResume方法

@Override //ViewCrawler.class
public void onActivityResumed(Activity activity) {
    installConnectionSensor(activity);
    mEditState.add(activity);
}
//通過一系列處理,最終調(diào)用了visit方法
/**
     * Scans the View hierarchy below rootView, applying it's operation to each matching child view.
     */
public void visit(View rootView) {
  Log.d(LOGTAG, mPath.get(0).toString());
  mPathfinder.findTargetsInRoot(rootView, mPath, this);
}

這種做法的效率暫且不停,到這里我們的流程就分析完了。大致的流程就是

App端 上傳屏幕截圖和頁面布局信息-》服務(wù)端操作之后,下發(fā)需要埋點(diǎn)的viewpath -》 app端存儲這個(gè)path,并在每個(gè)activity的onResume都去執(zhí)行尋找path的任務(wù)-》注冊Accessibility監(jiān)聽,上傳相應(yīng)事件。

這樣就實(shí)現(xiàn)了可視化埋點(diǎn),但是這種方式,應(yīng)該是用于已經(jīng)發(fā)布到線上的app的埋點(diǎn),而且不同版本不通用。。。因?yàn)関iew的id等信息是會隨著版本變化的。如果這里有錯(cuò)誤,請大神指出。不勝感激,謝謝??!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容