How would I put multiple Facebook tracking pixels on one web page?

后端 未结 7 1134
情书的邮戳
情书的邮戳 2020-12-30 11:36

We are trying to use Facebook\'s Ad tracking pixels to track ads. we looked at Facebook\'s documentation and that led me no where.

I need to know how to trigger mult

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-30 12:12

    Simply put in one JS file this code: //Facebook Multi-Pixel

        (function() {
            var fbq = (function() {
                function fbq()
                {
                    if(arguments.length > 0) {
                        var action, pixel_id, type_track, content_obj;
    
                    if( typeof arguments[0] == "string") action = arguments[0];
                    if( typeof arguments[1] == "string") pixel_id = arguments[1];
                    if( typeof arguments[2] == "string") type_track = arguments[2];
                    if( typeof arguments[3] == "object") content_obj = arguments[3];
    
                    var params = [];
                    if(typeof action == "string" && action.replace(/\s+/gi, "") != "" && 
                       typeof pixel_id == "string" && pixel_id.replace(/\s+/gi, "") != "" &&
                       typeof type_track == "string" && type_track.replace(/\s+/gi, "")) {
    
                        params.push("id=" + encodeURIComponent(pixel_id));
                        switch(type_track) {
                            case "PageView":
                            case "ViewContent":
                            case "Search":
                            case "AddToCart":
                            case "InitiateCheckout":
                            case "AddPaymentInfo":
                            case "Lead":
                            case "CompleteRegistration":
                            case "Purchase":
                            case "AddToWishlist":
                                params.push("ev=" + encodeURIComponent(type_track));
                                break;
                            default:
                                return;
                        }
    
                        params.push("dl=" + encodeURIComponent(document.location.href));
                        params.push("rl=" + encodeURIComponent(document.referrer));
                        params.push("if=false");
                        params.push("ts=" + new Date().getTime());
    
                        if(typeof content_obj == "object") {
                            for(var u in content_obj) {
                                if(typeof content_obj[u] == "object" && content_obj[u] instanceof Array) {
                                    if(content_obj[u].length > 0) {
                                        for(var y=0; y

    And than, usage example:

    fbq('track', "", "PageView");
    
    fbq('track", "", "ViewContent", {
                     content_name: "name test",
                     content_category: "category test",
                     content_ids: ["test"],
                     content_type: "product",
                     value: 7.99,
                     currency: "GBP"
              });
    

    and so on.

提交回复
热议问题