After years of evading capture, cops were finally able to nab the Golden State Killer this week thanks to some ingenious police work — which involved running DNA found at one of the crime scenes through genealogy websites and then matching it to one of his relatives.

The Sacramento County District Attorney’s Office revealed their clever tactics on Thursday to the Sacramento Bee.

They not only confirmed that the case was cracked online — like many law enforcement experts had suspected — but also disclosed how investigators scoured through dozens of family trees in an attempt to find an “ancestor” who matched the killer’s age and description.

When authorities busted Joseph James DeAngelo earlier this week, they said it was a DNA sample that ultimately led to his arrest, but refused to go into further detail.

Chief Deputy District Attorney Steve Grippi, however, was able to provide more answers while speaking to the Bee on Thursday.

He said it took investigators quite some time to figure out which bloodline to follow, though they were eventually able to crack the case by honing in on people who lived in areas where the Golden State Killer struck.

They then narrowed down their search to people who fit the same age and description as him — and checked to see if they matched the DNA sample that cops picked up at a crime scene years earlier.

DeAngelo’s name apparently came up after detectives stumbled upon one of his relatives, according to the Bee.

The 72-year-old used to live in areas where the GSK murders took place and was in the right age range, Grippi said. He was named a suspect last Thursday, with authorities conducting surveillance on his home for roughly six days before taking him into custody.

In order to confirm the DNA match, Grippi said investigators obtained another, more recent sample from a piece of trash that DeAngelo discarded — and then compared it to the one they found at the crime scene. It was a perfect match.

DeAngelo was arrested Tuesday and charged with six counts of murder. He is believed to have committed at least 12 killings and 50 rapes in 10 different counties across California between 1976 and 1986.