Go code to parse IAB v1/v2 consent based on github.com/LiveRamp/iabconsent library.
This package is just a wrapper on their amazing work to fill our needs.
go get github.com/hybridtheory/iab-tcf
package main
import (
"fmt"
iab "github.com/hybridtheory/iab-tcf"
)
func main() {
encoded := "COyt4MbOyt4MbMOAAAENAiCgAIAAAAAAAAAAADEAAgIAAAAAAAA.YAAAAAAAAAA"
consent, err := iab.NewConsent(encoded)
}
In order to validate if the CMP received belongs to a valid id there's a loader integrated with the library.
package main
import (
"fmt"
iab "github.com/hybridtheory/iab-tcf"
cmp "github.com/hybridtheory/iab-tcf/cmp"
)
func main() {
cmp.NewLoader().LoadIDs()
consent, err := iab.NewConsent(encoded)
consent.IsCMPValid() // This will return true/false depending on the ids loaded.
}
If instead of the default vendor list https://cmplist.consensu.org/v2/cmp-list.json we want to use our own, we can use an option:
err := cmp.NewLoader(cmp.WithURL("https://example.com/cmp-list.json")).LoadIDs()
The format of the JSON must be the same.
If we want to use our own list of valid CMPs we can simply set the variable:
cmp.ValidCMPs = []int{1, 123}
We use Ginkgo and Gomega for testing purposes.
ginkgo ./...