Google DFP: Pass UTM_Source to DFP with javascript

2019-01-29 13:15发布

问题:

I'm looking for a way to grab the utm_source value from a URL with javascript and pass it to Google DFP, which doesn't involve configuring anything in the DFP Admin (such as key-value targeting) as we don't have access to the admin panel for the specific site we are working with.

Would appreciate any tips / javascript code.

回答1:

Use setTargetting. You will need to access the Inventory tab in DFP's dashboard to create the utm code keys (Key-values link)

<script>

    function getParameterByName(name, url) {
        if (!url) url = window.location.href;
        name = name.replace(/[\[\]]/g, "\\$&");
        var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
            results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, " "));
    }

    googletag.cmd.push(function () {
        googletag.pubads().setTargeting('utm_source', getParameterByName('utm_source'));
    });

</script>